当前位置:   article > 正文

el-table表格分页时,最后一页添加合计行并自定义值_el-table合计栏分页

el-table合计栏分页
1.table合并行需求.

最近项目里提了个需求,之前没有接触过,搜索后写出个demo供自己记录一下.

最终效果:

1. 表格可以翻页,但只在最后一页时展示合计行.

2. 合计行的前i个单元格合并,并且赋值,最后n个单元格展示后台返回的值.

3. 合计行样式颜色修改.

 效果如上图.

因为是自己测试demo,没有后台配合,就自行填充了数据,后续改为接口调用就行.

2. html代码

具体的方法名跟属性可以在element-ui里找到,就不赘述了直接贴代码了哈.

  1. <template>
  2. <div>
  3. <el-button @click="changes">
  4. 展示
  5. </el-button>
  6. <el-table
  7. ref="tableRef"
  8. :data="tableData"
  9. :summary-method="getSummaries"
  10. :show-summary="dataFlag"
  11. :span-method="arraySpanMethod"
  12. border
  13. style="width: 100%">
  14. <el-table-column
  15. prop="id"
  16. label="ID"
  17. width="180">
  18. </el-table-column>
  19. <el-table-column
  20. prop="name"
  21. label="姓名">
  22. </el-table-column>
  23. <!-- 具体的table内容就根据自己的情况来写,就不全放上去了哈 -->
  24. </el-table>
  25. </div>
  26. </template>
3. 数据

        数据部分是我自行补充的,后续项目里可根据实际情况变更

  1. data() {
  2. return {
  3. dataFlag:true,
  4. dats:15,
  5. datas:20,
  6. customNum: 20202,
  7. customNums: 2020222,
  8. tableData: [],
  9. };
  10. },
4.方法

主要的方法,代码如下:

  1. methods: {
  2. changes() {
  3. this.dataFlag = !this.dataFlag;
  4. this.$nextTick(() => {
  5. if (this.$refs.tableRef.$el) {
  6. let current = this.$refs.tableRef.$el
  7. .querySelector(".el-table__footer-wrapper")
  8. .querySelector(".el-table__footer");
  9. let cell = current.rows[0].cells;
  10. for(let i = 1;i<8;i++) {
  11. cell[i].style.display = "none";
  12. }
  13. cell[0].colSpan = "8"; // 合并单元格
  14. cell[0].style.color = "red"; // 修改合计行某一个单元格的样式。
  15. }
  16. });
  17. },
  18. arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  19. this.$nextTick(() => {
  20. if (this.$refs.tableRef.$el) {
  21. let current = this.$refs.tableRef.$el
  22. .querySelector(".el-table__footer-wrapper")
  23. .querySelector(".el-table__footer");
  24. let cell = current.rows[0].cells;
  25. for(let i = 1;i<8;i++) {
  26. cell[i].style.display = "none";
  27. }
  28. cell[0].colSpan = "8"; // 合并单元格
  29. cell[0].style.color = "red"; // 修改合计行某一个单元格的样式。
  30. }
  31. });
  32. },
  33. getSummaries(param) {
  34. const { columns, data } = param;
  35. const sums = [];
  36. columns.forEach((column, index) => {
  37. if (index === 0) {
  38. sums[index] = `合计(税前折价为`+this.dats+`,税后折价计为`+this.datas+`)`;;
  39. return;
  40. }
  41. if (index === 1) {
  42. // this.customNum 自定义值一般为后台返回合计值
  43. sums[8] = this.customNum;
  44. sums[9] = this.customNums;
  45. return;
  46. }
  47. });
  48. return sums;
  49. },
  50. },
5.总结

以上就是一个简单的demo全部内容了,主要还是方法需要仔细了解一下,不然就像我自己搞了半天还是头大的很,所以记录下来以备后续回顾.

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

闽ICP备14008679号