tableItem.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. :header-cell-style="headerCellStyle"
  11. >
  12. <el-table-column
  13. v-if="isShowBox"
  14. type="selection"
  15. width="50"
  16. align="center"
  17. >
  18. </el-table-column>
  19. <template>
  20. <column-item
  21. v-for="item in col"
  22. :key="item.label"
  23. :col="item"
  24. ></column-item>
  25. </template>
  26. <template #empty>
  27. <div class="empty" style="height:12.5rem;">
  28. <span class="empty-desc">暂无数据</span>
  29. </div>
  30. </template>
  31. </el-table>
  32. </div>
  33. </template>
  34. <script>
  35. import ColumnItem from "./columnItem";
  36. export default {
  37. name: "TableItem",
  38. components: {
  39. ColumnItem
  40. },
  41. props: {
  42. // 表格数据
  43. tableData: {
  44. type: Array,
  45. default: () => []
  46. },
  47. // 树型结构表头数据
  48. col: {
  49. type: Array,
  50. default: () => []
  51. },
  52. // 是否在表格下方显示合计
  53. isShowSum: {
  54. type: Boolean,
  55. default: false
  56. },
  57. // 判断单元格文字是居中还是左对齐显示,默认居中
  58. alignType: {
  59. type: String,
  60. default: "center"
  61. },
  62. // 设置表格高度,固定表头
  63. height: {
  64. type: String,
  65. default: null // 默认不固定表头
  66. },
  67. // 判断是否显示多选框
  68. isShowBox: {
  69. type: Boolean,
  70. default: false // 默认不展示
  71. }
  72. },
  73. methods: {
  74. headerCellStyle({ row, column, rowIndex, columnIndex }) {
  75. let columnIndexList1 = [4];
  76. let columnIndexList2 = [5];
  77. let columnIndexList3 = [6];
  78. let columnIndexList4 = [7];
  79. if (columnIndexList1.includes(columnIndex) && rowIndex == 0) {
  80. //如果有多个css样式,使用;隔开
  81. return "color:#fff;cursor: pointer;background-color:red !important";
  82. }
  83. if (columnIndexList2.includes(columnIndex) && rowIndex == 0) {
  84. //如果有多个css样式,使用;隔开
  85. return "color:#fff;cursor: pointer;background-color:green !important";
  86. }
  87. if (columnIndexList3.includes(columnIndex) && rowIndex == 0) {
  88. //如果有多个css样式,使用;隔开
  89. return "color:#fff;cursor: pointer;background-color:blue !important";
  90. }
  91. if (columnIndexList4.includes(columnIndex) && rowIndex == 0) {
  92. //如果有多个css样式,使用;隔开
  93. return "color:#000;cursor: pointer;background-color:yellow !important ";
  94. }
  95. },
  96. flitterData(arr) {
  97. var spanOneArr = [];
  98. let concatOne = 0;
  99. arr.forEach((item, index) => {
  100. if (index === 0) {
  101. spanOneArr.push(1);
  102. } else {
  103. // name 修改
  104. if (item.batchId === arr[index - 1].batchId) {
  105. // 第一列需合并相同内容的判断条件
  106. spanOneArr[concatOne] += 1;
  107. spanOneArr.push(0);
  108. } else {
  109. spanOneArr.push(1);
  110. concatOne = index;
  111. }
  112. }
  113. });
  114. return {
  115. one: spanOneArr
  116. };
  117. },
  118. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  119. if (columnIndex === 0) {
  120. const _row = this.flitterData(this.tableData).one[rowIndex];
  121. const _col = _row > 0 ? 1 : 0;
  122. return {
  123. rowspan: _row,
  124. colspan: _col
  125. };
  126. }
  127. if (columnIndex === 1) {
  128. // this.tableData 修改
  129. const _row = this.flitterData(this.tableData).one[rowIndex];
  130. const _col = _row > 0 ? 1 : 0;
  131. return {
  132. rowspan: _row,
  133. colspan: _col
  134. };
  135. }
  136. if (columnIndex === 4) {
  137. // this.tableData 修改
  138. const _row = this.flitterData(this.tableData).one[rowIndex];
  139. const _col = _row > 0 ? 1 : 0;
  140. return {
  141. rowspan: _row,
  142. colspan: _col
  143. };
  144. }
  145. if (columnIndex === 2) {
  146. // this.tableData 修改
  147. const _row = this.flitterData(this.tableData).one[rowIndex];
  148. const _col = _row > 0 ? 1 : 0;
  149. return {
  150. rowspan: _row,
  151. colspan: _col
  152. };
  153. }
  154. if (columnIndex === 3) {
  155. // this.tableData 修改
  156. const _row = this.flitterData(this.tableData).one[rowIndex];
  157. const _col = _row > 0 ? 1 : 0;
  158. return {
  159. rowspan: _row,
  160. colspan: _col
  161. };
  162. }
  163. if (row.type === 1) {
  164. return {
  165. rowspan: 1,
  166. colspan: 17
  167. };
  168. }
  169. }
  170. }
  171. };
  172. </script>
  173. <style>
  174. /* 处理表格表头和内容错位问题 */
  175. .el-table th.gutter {
  176. display: table-cell !important;
  177. }
  178. .el-table th,
  179. .el-table td {
  180. padding: 0.4375rem 0 !important;
  181. }
  182. </style>