当前位置:   article > 正文

ElementUI el-table 的自定义合计行及自定义合并单元格_el-table 自定义行

el-table 自定义行
1.html代码
  1. <el-table :data="tableData" border style="width: 100%" :span-method="handleSpanMethod" ref="table" :summary-method="summaryTotal" show-summary height="63vh">
  2. <el-table-column prop="companyName" label="物业公司" width=""></el-table-column>
  3. <el-table-column prop="organName" label="管理区" width=""></el-table-column>
  4. <el-table-column prop="houseName" label="楼栋"> </el-table-column>
  5. <el-table-column prop="resourceName" label="房间"> </el-table-column>
  6. <el-table-column prop="paymentDate" label="收款日期"> </el-table-column>
  7. <el-table-column prop="receiptNo" label="收款单号"> </el-table-column>
  8. <el-table-column prop="standardName" label="收费标准"> </el-table-column>
  9. <el-table-column prop="customerName" label="客户名称"> </el-table-column>
  10. <el-table-column prop="createByName" label="收款人"> </el-table-column>
  11. <el-table-column prop="actualAmount" label="实收金额"> </el-table-column>
  12. <el-table-column prop="discountAmount" label="优惠金额"> </el-table-column>
  13. <el-table-column prop="" label="冲抵金额"> </el-table-column>
  14. <el-table-column prop="" label="单据状态"> </el-table-column>
  15. </el-table>
2.js部分代码
  1. //自定义表格合计行
  2. summaryTotal(param){
  3. let sums = []
  4. sums[0] = '合计'
  5. sums[9] = this.sumDetails.actualAmount
  6. sums[10] = this.sumDetails.discountAmount
  7. console.log('nums',sums);
  8. return sums
  9. },
  10. //合并单元格的逻辑
  11. //此表格有5列需要合并的,所以这里有5个数组
  12. getSpanArr() {
  13. this.tableOneArr = [];
  14. this.tableOnePos = 0;
  15. this.tableTwoArr = [];
  16. this.tableTwoPos = 0;
  17. this.tableThreeArr = [];
  18. this.tableThreePos = 0;
  19. this.tableFourArr = [];
  20. this.tableFourPos = 0;
  21. this.tableFiveArr = [];
  22. this.tableFivePos = 0;
  23. for (var i = 0; i < this.tableData.length; i++) {
  24. if (i === 0) {
  25. // 第一行必须存在
  26. this.tableOneArr.push(1);
  27. this.tableOnePos = 0;
  28. this.tableTwoArr.push(1);
  29. this.tableTwoPos = 0;
  30. this.tableThreeArr.push(1);
  31. this.tableThreePos = 0;
  32. this.tableFourArr.push(1);
  33. this.tableFourPos = 0;
  34. this.tableFiveArr.push(1);
  35. this.tableFivePos = 0;
  36. } else {
  37. // 判断当前元素与上一个元素是否相同
  38. if (this.tableData[i].companyCode === this.tableData[i - 1].companyCode) {
  39. this.tableOneArr[this.tableOnePos] += 1;
  40. this.tableOneArr.push(0);
  41. } else {
  42. this.tableOneArr.push(1);
  43. this.tableOnePos = i;
  44. }
  45. // 判断当前元素与上一个元素是否相同
  46. if (this.tableData[i].organName === this.tableData[i - 1].organName) {
  47. this.tableTwoArr[this.tableTwoPos] += 1;
  48. this.tableTwoArr.push(0);
  49. } else {
  50. this.tableTwoArr.push(1);
  51. this.tableTwoPos = i;
  52. }
  53. // 判断当前元素与上一个元素是否相同
  54. if (this.tableData[i].houseCode === this.tableData[i - 1].houseCode) {
  55. this.tableThreeArr[this.tableThreePos] += 1;
  56. this.tableThreeArr.push(0);
  57. } else {
  58. this.tableThreeArr.push(1);
  59. this.tableThreePos = i;
  60. }
  61. // 判断当前元素与上一个元素是否相同
  62. if (this.tableData[i].resourceCode === this.tableData[i - 1].resourceCode) {
  63. this.tableFourArr[this.tableFourPos] += 1;
  64. this.tableFourArr.push(0);
  65. } else {
  66. this.tableFourArr.push(1);
  67. this.tableFourPos = i;
  68. }
  69. // 判断当前元素与上一个元素是否相同
  70. if (this.tableData[i].paymentDate === this.tableData[i - 1].paymentDate) {
  71. this.tableFiveArr[this.tableFivePos] += 1;
  72. this.tableFiveArr.push(0);
  73. } else {
  74. this.tableFiveArr.push(1);
  75. this.tableFivePos = i;
  76. }
  77. }
  78. }
  79. },
  80. //动态合并行
  81. handleSpanMethod({ row, column, rowIndex, columnIndex }){
  82. // 第一列的合并方法,省
  83. if (columnIndex == 0 ) {
  84. const _row = this.tableOneArr[rowIndex]
  85. const _col = _row > 0 ? 1 : 0 // 如果被合并了_row=0则它这个列需要取消
  86. return {
  87. rowspan: _row,
  88. colspan: _col
  89. }
  90. }
  91. else if (columnIndex == 1 ) {
  92. const _row = this.tableTwoArr[rowIndex]
  93. const _col = _row > 0 ? 1 : 0 // 如果被合并了_row=0则它这个列需要取消
  94. return {
  95. rowspan: _row,
  96. colspan: _col
  97. }
  98. }else if (columnIndex == 2 ) {
  99. const _row = this.tableThreeArr[rowIndex]
  100. const _col = _row > 0 ? 1 : 0 // 如果被合并了_row=0则它这个列需要取消
  101. return {
  102. rowspan: _row,
  103. colspan: _col
  104. }
  105. }else if (columnIndex == 3 ) {
  106. const _row = this.tableFourArr[rowIndex]
  107. const _col = _row > 0 ? 1 : 0 // 如果被合并了_row=0则它这个列需要取消
  108. return {
  109. rowspan: _row,
  110. colspan: _col
  111. }
  112. }else if (columnIndex == 4 ) {
  113. const _row = this.tableFiveArr[rowIndex]
  114. const _col = _row > 0 ? 1 : 0 // 如果被合并了_row=0则它这个列需要取消
  115. return {
  116. rowspan: _row,
  117. colspan: _col
  118. }
  119. }
  120. },
3.注意

 这里我表格使用了heigth="63vh",固定了高度,开启合并行的时候合并行没有显示出来,需要给表格加上 ref="table',再在生命周期update函数里加上以下代码

  1. updated (){
  2. this.$nextTick( () => {
  3. this.$refs['table'].doLayout()
  4. })
  5. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/686724
推荐阅读
相关标签
  

闽ICP备14008679号