tableItem.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div>
  3. <el-table
  4. :show-summary='isShowSum'
  5. :data='tableData'
  6. :height='height'
  7. style='width: 100%'
  8. :span-method="objectSpanMethod"
  9. :border="true"
  10. >
  11. <el-table-column
  12. v-if='isShowBox'
  13. type='selection'
  14. width='50'
  15. align='center'
  16. >
  17. </el-table-column>
  18. <template>
  19. <column-item
  20. v-for='item in col'
  21. :key='item.label'
  22. :col='item'
  23. ></column-item>
  24. </template>
  25. <template #empty>
  26. <div class='empty' style='height:12.5rem;'>
  27. <span class='empty-desc'>暂无数据</span>
  28. </div>
  29. </template>
  30. </el-table>
  31. </div>
  32. </template>
  33. <script>
  34. import ColumnItem from './columnItem'
  35. export default {
  36. name: 'TableItem',
  37. components: {
  38. ColumnItem
  39. },
  40. props: {
  41. // 表格数据
  42. tableData: {
  43. type: Array,
  44. default: () => []
  45. },
  46. // 树型结构表头数据
  47. col: {
  48. type: Array,
  49. default: () => []
  50. },
  51. // 是否在表格下方显示合计
  52. isShowSum: {
  53. type: Boolean,
  54. default: false
  55. },
  56. // 判断单元格文字是居中还是左对齐显示,默认居中
  57. alignType: {
  58. type: String,
  59. default: 'center'
  60. },
  61. // 设置表格高度,固定表头
  62. height: {
  63. type: String,
  64. default: null// 默认不固定表头
  65. },
  66. // 判断是否显示多选框
  67. isShowBox: {
  68. type: Boolean,
  69. default: false // 默认不展示
  70. }
  71. },
  72. methods: {
  73. flitterData (arr) {
  74. var spanOneArr = []
  75. let concatOne = 0
  76. arr.forEach((item, index) => {
  77. if (index === 0) {
  78. spanOneArr.push(1)
  79. } else {
  80. // name 修改
  81. if (item.batchId === arr[index - 1].batchId) { // 第一列需合并相同内容的判断条件
  82. spanOneArr[concatOne] += 1
  83. spanOneArr.push(0)
  84. } else {
  85. spanOneArr.push(1)
  86. concatOne = index
  87. }
  88. }
  89. })
  90. return {
  91. one: spanOneArr
  92. }
  93. },
  94. objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
  95. if (columnIndex === 0) {
  96. const _row = (this.flitterData(this.tableData).one)[rowIndex]
  97. const _col = _row > 0 ? 1 : 0
  98. return {
  99. rowspan: _row,
  100. colspan: _col
  101. }
  102. }
  103. if (columnIndex === 1) {
  104. // this.tableData 修改
  105. const _row = (this.flitterData(this.tableData).one)[rowIndex]
  106. const _col = _row > 0 ? 1 : 0
  107. return {
  108. rowspan: _row,
  109. colspan: _col
  110. }
  111. }
  112. if (columnIndex === 2) {
  113. // this.tableData 修改
  114. const _row = (this.flitterData(this.tableData).one)[rowIndex]
  115. const _col = _row > 0 ? 1 : 0
  116. return {
  117. rowspan: _row,
  118. colspan: _col
  119. }
  120. }
  121. if (row.type === 1) {
  122. return {
  123. rowspan: 1,
  124. colspan: 17
  125. }
  126. }
  127. }
  128. }
  129. }
  130. </script>
  131. <style>
  132. /* 处理表格表头和内容错位问题 */
  133. .el-table th.gutter {
  134. display: table-cell !important;
  135. }
  136. .el-table th,
  137. .el-table td {
  138. padding: 0.4375rem 0 !important;
  139. }
  140. </style>