Vue3 el-table 如何动态合并单元格

<template>
  <div>
    <el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      :header-cell-style="{ textAlign: 'center' }"
    >
      <el-table-column prop="area" label="区域" align="center" />
      <el-table-column prop="province" label="省份" align="center" />
      <el-table-column prop="month_1" label="一月" align="center" />
      <el-table-column prop="month_2" label="二月" align="center" />
      <el-table-column prop="month_3" label="三月" align="center" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import {
  ref,
  toRefs,
  reactive,
  nextTick,
  computed,
  watch,
  onMounted
} from "vue";
const tableData = [
  // 一年级
  {
    area: "华北",
    province: "北京",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "华北",
    province: "北京",
    month_1: "200",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "华北",
    province: "山西",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "华北",
    province: "辽宁",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "东北",
    province: "吉林",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "东北",
    province: "黑2",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "东北",
    province: "黑2",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "东北",
    province: "黑2",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "西北",
    province: "黑2",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "西北",
    province: "西二",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  },
  {
    area: "西北",
    province: "西三",
    month_1: "100",
    month_2: "张三",
    month_3: "90"
  }
];
//存储合并单元格的开始位置
const setTabelRowSpan = (tableData: any, fieldArr: any, effectMerge = {}) => {
  let lastItem = {};
  fieldArr.forEach((field: any, index: any) => {
    let judgeArr = fieldArr.slice(0, index + 1);
    //@ts-ignore
    if (effectMerge[field]) {
      //@ts-ignore
      judgeArr = [...effectMerge[field], field]; //[area,field]
      console.log(judgeArr, "--==judgeAr2222");
    }
    tableData.forEach((item: any) => {
      item.mergeCell = fieldArr;
      const rowSpan = `rowspan_${field}`;
      // 判断是否合并到上个单元格。
      //@ts-ignore
      if (
        judgeArr.every((e: any) => lastItem[e] === item[e] && item[e] !== "")
      ) {
        // 判断是否所在行的列对应的值全部相同,并且此列的值不为空
        // 是:合并行
        item[rowSpan] = 0;
        //@ts-ignore
        lastItem[rowSpan] += 1;
      } else {
        // 否:完成一次同类合并。lastItem重新赋值,进入下一次合并计算。
        item[rowSpan] = 1;
        lastItem = item;
      }
    });
  });
};
//@ts-ignore
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (row.mergeCell.includes(column.property)) {
    const rowspan = row[`rowspan_${column.property}`];
    if (rowspan) {
      return { rowspan: rowspan, colspan: 1 };
    } else {
      return { rowspan: 0, colspan: 0 };
    }
  }
};
onMounted(() => {
  const effectMerge = {
    month_1: ["area"], //一月参照区域进行合并
    month_3: ["area"] //三月参照区域合并
  };
  const arr = ["area", "province", "month_1", "month_2", "month_3"]; //全部字段
  setTabelRowSpan(tableData, arr, effectMerge); // (表格数据,表格字段,合并的条件)
});
</script>

<style></style>

效果图:

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现Vue3 Element-Plus的el-table汇总行单元格合并,你可以使用`span-method`属性来定义一个函数,该函数可以返回每个单元格需要合并的行数和列数。下面是一个示例代码: ```html <el-table :data="tableData" border style="width: 100%"> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> <el-table-column label="合计" :span-method="objectSpanMethod"> <template #default="{ rows, row, column, $index }"> <span v-if="$index === 0">{{ row.total }}</span> </template> </el-table-column> </el-table> ``` 在上面的代码中,我们定义了一个`objectSpanMethod`方法,并将其赋值给`span-method`属性。这个方法接收四个参数:`{ rows, row, column, $index }`。其中`rows`是当前列的所有行数据,`row`是当前行数据,`column`是当前列数据,`$index`是当前行的索引。 下面是`objectSpanMethod`方法的实现: ```js methods: { objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 3) { if (rowIndex === 0) { return { rowspan: this.tableData.length, colspan: 1 }; } else { return { rowspan: 0, colspan: 0 }; } } } } ``` 在上面的代码中,我们检查当前列是否为第四列(即“合计”列),如果是,我们检查当前行是否为第一行。如果是,我们返回一个对象`{ rowspan: this.tableData.length, colspan: 1 }`,其中`rowspan`表示当前单元格需要合并的行数,`colspan`表示当前单元格需要合并的列数。如果当前行不是第一行,则返回一个空对象`{ rowspan: 0, colspan: 0 }`,表示当前单元格不需要合并。 这样,我们就可以实现Vue3 Element-Plus的el-table汇总行单元格合并了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值