element-plus(el-table)中summary-method和span-method用法详解

el-table官方文档

el-table属性名说明类型参数
summary-method自定义的合计计算方法Function

columns, data

span-method合并行或列的计算方法Functionrow, column, rowIndex, columnIndex

合并表格行(通用)

const mergeObj = ref({}); // 用来记录需要合并行的下标
const mergeArr = ref(["statename"]); // 表格中的要合并列名
const tableData=ref([]);//网格数据

//修改网格数据源
const mergeFun=()=>{
   mergeArr.value.forEach((key, index1) => {
      let count = 0; // 用来记录需要合并行的起始位置
      mergeObj.value[key] = []; // 记录每一列的合并信息
      tableData.value.forEach((item, index) => {
        // index == 0表示数据为第一行,直接 push 一个 1
        if (index === 0) {
          mergeObj.value[key].push(1);
        } else {
 // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
          if (item[key] === tableData.value[index - 1][key]) {
            mergeObj.value[key][count] += 1;
            mergeObj.value[key].push(0);
          } else {
            // 如果当前行和上一行其值不相等
            count = index; // 记录当前位置
            mergeObj.value[key].push(1); // 重新push 一个 1
          }
        }
      });
    });
}

const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
  // 判断列的属性
   if (mergeArr.value.indexOf(column.property) !== -1) {
     // 判断其值是不是为0
     if (mergeObj.value[column.property][rowIndex]) {
       return [mergeObj.value[column.property][rowIndex], 1]
     } else {
       // 如果为0则为需要合并的行
       return [0, 0];
     }
   }
};

//调用mergeFun()

合并表格底部行(“合计行”)中的某几列

const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 1) {
      sums[index] = "合计";
      return;
    } 
    const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => Number.isNaN(value))) {
        sums[index] = `${values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!Number.isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0)}`;
      } else {
        sums[index] = "";
      }
  });
  return sums;
};

//合并列
const spanMethod = (row, column, rowIndex, columnIndex) => {
  nextTick(() => {
    if (table.value.$el) {
      let current = table.value.$el
        .querySelector(".el-table__footer-wrapper")
        .querySelector(".el-table__footer");
      let cell = current.rows[0].cells;
      //修改样式
      cell[1].style.textAlign = "center";
      // 合并单元格
      cell[2].style.display = "none";
      cell[1].colSpan = "2";
    }
  });
};

持续更新中。。。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@李优秀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值