index.vue 8.9 KB

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