index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <div v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)" class="table-wrapper">
  3. <el-table ref="table" v-el-table-infinite-scroll="load" :data="dealedTableData" :header-cell-class-name="headerCellClass" :row-class-name="rowClass" :cell-class-name="cellClass" :span-method="tableSpanMethod" :show-summary="showSummary" :summary-method="tableSummaryMethod" :height="height" stripe fit border @cell-click="cellClickHandler">
  4. <el-table-column v-for="col in filteredTableCols" :key="col.pagecode" :prop="col.pagecode" :label="col.pagename" :width="col.displaywidth" :show-overflow-tooltip="showOverflowTooltip" :formatter="tableFormatter">
  5. <template #header>
  6. <el-tooltip :content="col.pagedescribe || col.pagename" placement="top">
  7. <TableHeaderCell :label="col.pagename" :filter-options="tableDataFilters[col.pagecode]" :filter-values.sync="filterValues[col.pagecode]" filter-style="arrow" :sortable="!!col.enablesort" :sort-rule.sync="tableDataSortRules[col.pagecode]" />
  8. </el-tooltip>
  9. </template>
  10. <template slot-scope="scope">
  11. <span v-if="!col.backgroundcolorexpression">{{ scope.row[col.pagecode] }}</span>
  12. <div v-else :class="isTrue(scope.row[col.pagecode],col.backgroundcolorexpression )"></div>
  13. </template>
  14. </el-table-column>
  15. <el-table-column v-if="dealedTableData.length && tableBtns.length" fixed="right" width="240" label="操作">
  16. <template slot-scope="scope">
  17. <div class="hd-td">
  18. <el-scrollbar style="height: 100%">
  19. <AuthButton v-for="(item,index) in tableBtns" :key="index" :auth="item" :row="scope.row" />
  20. </el-scrollbar>
  21. </div>
  22. </template>
  23. </el-table-column>
  24. </el-table>
  25. </div>
  26. </template>
  27. <script>
  28. import TableHeaderCell from '../TableHeaderCell'
  29. import AuthButton from '@/components/AuthButton'
  30. import { setTableFilters } from '@/utils/table'
  31. import { mapGetters } from 'vuex'
  32. export default {
  33. name: 'SimpleTable',
  34. components: { TableHeaderCell, AuthButton },
  35. props: {
  36. loading: {
  37. type: Boolean,
  38. default: false
  39. },
  40. height: {
  41. type: [String, Number],
  42. default: '100%'
  43. },
  44. tableCols: {
  45. type: Array,
  46. default: () => []
  47. },
  48. tableBtns: {
  49. type: Array,
  50. default: () => []
  51. },
  52. data: {
  53. type: Array,
  54. default: () => []
  55. },
  56. headerCellClassName: {
  57. type: Function
  58. },
  59. rowClassName: {
  60. type: Function
  61. },
  62. cellClassName: {
  63. type: Function
  64. },
  65. spanMethod: {
  66. type: Function
  67. },
  68. // 是否显示合计行
  69. showSummary: {
  70. type: Boolean,
  71. default: false
  72. },
  73. summaryMethod: {
  74. type: Function
  75. },
  76. // 不换行,溢出隐藏
  77. showOverflowTooltip: {
  78. type: Boolean,
  79. default: true
  80. },
  81. formatter: {
  82. type: Function
  83. }
  84. },
  85. data () {
  86. return {
  87. tableData: [],
  88. tableDataFilters: {}, // 表头-下拉数据
  89. filterValues: {}, // 表头-下拉-选中数据
  90. tableDataSortRules: {}, // 表头-排序
  91. tableGroups: [], // 表格分组规则
  92. spanArr: [] // 表格分组数据缓存
  93. }
  94. },
  95. computed: {
  96. ...mapGetters(['clickedCells']),
  97. filteredTableCols () {
  98. return this.tableCols.filter(col => col.isdisplay != 0)
  99. },
  100. dealedTableData () {
  101. const filtered = this.tableData.filter(item => {
  102. let flag = true
  103. Object.entries(this.filterValues).forEach(([key, arr]) => {
  104. if (arr.length && !arr.includes(String(item[key]))) {
  105. flag = false
  106. }
  107. })
  108. return flag
  109. })
  110. const sortRules = Object.entries(this.tableDataSortRules).reduce(
  111. (pre, [key, value]) => {
  112. if (value) {
  113. pre[0].push(key)
  114. value = value === 'ascending' ? 'asc' : 'desc'
  115. pre[1].push(value)
  116. }
  117. return pre
  118. },
  119. [[], []]
  120. )
  121. return this._.orderBy(filtered, sortRules[0], sortRules[1])
  122. }
  123. },
  124. watch: {
  125. data: {
  126. handler (arr) {
  127. this.tableData = arr
  128. },
  129. deep: true
  130. },
  131. tableData: {
  132. handler () {
  133. this.setTableFilters()
  134. },
  135. deep: true
  136. },
  137. tableCols: {
  138. handler () {
  139. this.setTableFilters()
  140. },
  141. deep: true
  142. },
  143. dealedTableData: {
  144. handler (arr) {
  145. const spanArr = []
  146. let pos = 0
  147. arr.forEach((item, index, arr) => {
  148. if (index === 0) {
  149. spanArr.push(1)
  150. } else {
  151. if (this.tableGroups.every(prop => arr[index][prop] === arr[index - 1][prop])) {
  152. spanArr[pos] += 1
  153. spanArr.push(0)
  154. } else {
  155. spanArr.push(1)
  156. pos = index
  157. }
  158. }
  159. })
  160. this.spanArr = spanArr
  161. },
  162. deep: true
  163. },
  164. headerCellClassName: {
  165. handler (func) {
  166. if (func) {
  167. this.headerCellClass = func().bind(this)
  168. }
  169. },
  170. deep: true,
  171. immediate: true
  172. },
  173. rowClassName: {
  174. handler (func) {
  175. if (func) {
  176. this.rowClass = func().bind(this)
  177. }
  178. },
  179. deep: true,
  180. immediate: true
  181. },
  182. cellClassName: {
  183. handler (func) {
  184. if (func) {
  185. this.cellClass = func().bind(this)
  186. }
  187. },
  188. deep: true,
  189. immediate: true
  190. },
  191. spanMethod: {
  192. handler (func) {
  193. if (func) {
  194. this.tableSpanMethod = func().bind(this)
  195. }
  196. },
  197. deep: true,
  198. immediate: true
  199. },
  200. summaryMethod: {
  201. handler (func) {
  202. if (func) {
  203. this.tableSummaryMethod = func().bind(this)
  204. }
  205. },
  206. deep: true,
  207. immediate: true
  208. },
  209. formatter: {
  210. handler (func) {
  211. if (func) {
  212. this.tableFormatter = func().bind(this)
  213. }
  214. },
  215. deep: true,
  216. immediate: true
  217. }
  218. },
  219. mounted () {
  220. this.$emit('mounted', 'table', this.$refs['table'])
  221. },
  222. updated () {
  223. this.$refs['table']?.doLayout()
  224. },
  225. methods: {
  226. isTrue(row,data){
  227. return eval(data)
  228. },
  229. load () {
  230. this.$emit('load')
  231. },
  232. headerCellClass ({ row, column, rowIndex, columnIndex }) {
  233. const classes = []
  234. const rule = this.tableDataSortRules[column.property]
  235. if (rule) {
  236. classes.push(rule)
  237. }
  238. return classes.join(' ')
  239. },
  240. rowClass () {
  241. return ''
  242. },
  243. cellClass () {
  244. return ''
  245. },
  246. tableSpanMethod ({ row, column, rowIndex, columnIndex }) {
  247. if (this.tableGroups.includes(column.property)) {
  248. const _row = this.spanArr[rowIndex]
  249. const _col = _row > 0 ? 1 : 0
  250. return {
  251. rowspan: _row,
  252. colspan: _col
  253. }
  254. }
  255. },
  256. tableSummaryMethod (param) {
  257. const { columns, data } = param
  258. const sums = []
  259. columns.forEach((column, index) => {
  260. this.tableCols.forEach(p => {
  261. if (column.property === p.pagecode && p.enablecount) {
  262. const values = data.map(item => Number(item[column.property]))
  263. if (!values.every(value => isNaN(value))) {
  264. sums[index] = values.reduce((prev, curr) => {
  265. const value = Number(curr)
  266. if (!isNaN(value)) {
  267. return prev + curr
  268. } else {
  269. return prev
  270. }
  271. }, 0)
  272. sums[index] += ''
  273. }
  274. }
  275. })
  276. })
  277. return sums
  278. },
  279. cellClickHandler (...params) {
  280. this.$emit('cell-click', ...params)
  281. },
  282. tableFormatter (row, column, cellValue) {
  283. return cellValue
  284. },
  285. setTableFilters () {
  286. this.tableDataFilters = {}
  287. this.filteredTableCols.forEach(col => {
  288. if (col.enablefilter) {
  289. this.tableDataFilters[col.pagecode] = []
  290. }
  291. if (col.enablegroup) {
  292. this.tableGroups.push(col.pagecode)
  293. }
  294. })
  295. setTableFilters(this.tableData, this.tableDataFilters)
  296. },
  297. //表格-查看
  298. handleDetail (row) {
  299. this.$emit('handleDetail', row)
  300. },
  301. //表格-编辑
  302. handleEdit (row) {
  303. this.$emit('handleEdit', row)
  304. },
  305. //表格-其他类型按钮操作
  306. handleOther (row, auth) {
  307. this.$emit('handleOther', row, auth)
  308. },
  309. //表格-删除
  310. handleRemove (row) {
  311. this.$emit('handleRemove', row)
  312. }
  313. }
  314. }
  315. </script>
  316. <style lang="scss" scoped>
  317. .table-wrapper {
  318. width: 100%;
  319. height: 100%;
  320. ::v-deep .el-table {
  321. width: 100%;
  322. .cell {
  323. padding: 0;
  324. text-align: center;
  325. font-size: 14px;
  326. font-family: Helvetica, "Microsoft YaHei";
  327. letter-spacing: 0;
  328. .success{
  329. width:15px;
  330. height:15px;
  331. background:#0fc956;
  332. margin-left: calc(50% - 8px);
  333. }
  334. .error{
  335. width:15px;
  336. height:15px;
  337. background:#c92b0f;
  338. margin-left: calc(50% - 8px);
  339. }
  340. }
  341. .cell-click {
  342. cursor: pointer;
  343. color: #2d7cff;
  344. &.cell-clicked {
  345. color: purple;
  346. }
  347. }
  348. .el-table__header-wrapper {
  349. .cell {
  350. font-weight: bold;
  351. color: #101116;
  352. > .el-checkbox {
  353. display: none;
  354. }
  355. }
  356. }
  357. .hd-td {
  358. white-space: nowrap;
  359. width: 100%;
  360. padding: 0 10px;
  361. }
  362. }
  363. }
  364. .no-column {
  365. padding-top: 20px;
  366. background-color: #ffffff;
  367. color: #909399;
  368. text-align: center;
  369. font-size: 20px;
  370. font-family: Helvetica, "Microsoft YaHei";
  371. }
  372. </style>