vue element-ui合并单元格 合并指定列的行

参考文章:element-ui element-plus合并行、el-table 行合并,可以指定要合并行的列名称_东明之羞的博客-CSDN博客

基于上文对方法做了扩展(拓展部分已设置高亮说明)

项目vue版本vue3,其他版本自测

最终效果,el-table可以按照指定列字段合并单元格,并设置其他列相同规则
在这里插入图片描述
el-table-span-method.js

将el-table-span-method.js放置在vue项目的src\utils目录

/**
 * 合并相同数据,导出合并行所需的方法(只适合el-table)
 * @param {Array} dataArray el-table表数据源
 * @param {Array} mergeRowProp 合并行的列prop
 * @param {Array} sameRuleRowProp 相同合并规则行的列prop
 */
export function getSpanMethod(dataArray, mergeRowProp, sameRuleRowProp) {

    /**
     * 要合并行的数据
     */
    const rowspanNumObject = {};

    //初始化 rowspanNumObject
    mergeRowProp.map(item => {
        rowspanNumObject[item] = new Array(dataArray.length).fill(1, 0, 1).fill(0, 1);
        rowspanNumObject[`${item}-index`] = 0;
    });

    //计算相关的合并信息
    for (let i = 1; i < dataArray.length; i++) {
        mergeRowProp.map(key => {
            const index = rowspanNumObject[`${key}-index`];
            if (dataArray[i][key] === dataArray[i - 1][key]) {
                rowspanNumObject[key][index]++;
            } else {
                rowspanNumObject[`${key}-index`] = i;
                rowspanNumObject[key][i] = 1;
            }

        });
    }

    /**
     * 添加同规则合并行的数据
     */
    if (sameRuleRowProp !== undefined) {
        let k = Object.keys(rowspanNumObject).filter(key => {
            if (!key.includes('index')) {
                return key
            }
        })[0]
        for (let prop of sameRuleRowProp) {
            rowspanNumObject[prop] = rowspanNumObject[k]
            rowspanNumObject[`${prop}-index`] = rowspanNumObject[`${k}-index`]
            mergeRowProp.push(prop)
        }
    }

    /**
     * 导出合并方法
     */
    const spanMethod = function ({row, column, rowIndex, columnIndex}) {
        if (mergeRowProp.includes(column['property'])) {
            const rowspan = rowspanNumObject[column['property']][rowIndex];
            if (rowspan > 0) {
                return {rowspan: rowspan, colspan: 1}
            }
            return {rowspan: 0, colspan: 0}
        }
        return {rowspan: 1, colspan: 1}
    };

    return spanMethod;
}

使用示例

在vue页面中引用方法文件,并在的export default data() return中创建表格数据,写出计算合并单元格方法

<script>
import {getSpanMethod} from "@/utils/el-table-span-method";

export default {
  data() {
    return {
      orderData: [
        {
          id: "1",
          name: "王小虎",
          amount1: "234",
          amount2: "165"
        },
        {
          id: "1",
          name: "王小虎",
          amount1: "165",
          amount2: "165"
        },
        {
          id: "2",
          name: "王小虎",
          amount1: "324",
          amount2: "165"
        },
        {
          id: "2",
          name: "王小虎",
          amount1: "621",
          amount2: "165"
        },
        {
          id: "2",
          name: "王小虎",
          amount1: "539",
          amount2: "165"
        }
      ],
    };
  },
  computed:{
    spanMethod : {
      get() {
        return this.spanMethod = getSpanMethod(this.orderData,['id'],['name','amount2']);
      },
      set(val) {
        return val;
      }
    }
  }
}
</script>

el-table标签中设置表格数据,添加span-method方法

    <el-table :data="orderData" :span-method="spanMethod" border >
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="100"></el-table-column>
      <el-table-column prop="amount1" label="数值 1(元)"></el-table-column>
      <el-table-column prop="amount2" label="数值 2(元)"></el-table-column>
    </el-table>
Logo

前往低代码交流专区

更多推荐