Powered by md-Blog  文 - 篇  访客 -

Element-UI表格Table多列相同数据进行动态数据合并


  分类:Vue  / 
更新:2022-10-04 11:44:21  /  创建:2022-10-04 11:44:21
不要删除

看效果

img

上源码

<template>
<div>
<el-table :data="tableData" :span-method="objectSpanMethod" border style="width: 100%; margin-top: 20px" :cell-style="{background: '#FFF'}">
  <el-table-column width="120" prop="t1Name" label="一级标题" align="center">
  </el-table-column>
  <el-table-column width="140" prop="t2Name" label="二级标题" align="center" >
  </el-table-column>
  <el-table-column width="140" prop="t3Name" label="三级标题" align="center">
  </el-table-column>
  <el-table-column width="140" prop="text" label="说明" align="center">
  </el-table-column>
</el-table>
</div>
</template>

<script>
export default {
  data() {
    return {
      spanArr1: [],
      spanArr2: [],
      spanArr3: [],
      pos1: 0,
      pos2: 0,
      pos3: 0,
      tableData: [{
        t1Name: 'AAA',
        t2Name: 'BBB',
        t3Name: 'CCC',
        text: '测试1',
      }, {
        t1Name: 'AAA',
        t2Name: 'BBB',
        t3Name: 'XXX',
        text: '测试2',
      }, {
        t1Name: 'AAA',
        t2Name: 'CCC',
        t3Name: 'DDD',
        text: '测试3',
      }, {
        t1Name: 'AAA',
        t2Name: 'CCC',
        t3Name: 'DDD',
        text: '测试4',
      }]
    }
  },
  mounted() {
    this.getSpanArr(this.tableData);
  },
  methods: {
    // 合并单元格
    getSpanArr(data) {
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr1.push(1);
          this.spanArr2.push(1);
          this.spanArr3.push(1);
          this.pos1 = 0;
          this.pos2 = 0;
          this.pos3 = 0;
        } else {
          // 判断二级标题
          if (data[i].t1Name === data[i - 1].t1Name) {
            this.spanArr1[this.pos1] += 1;
            this.spanArr1.push(0);
            // 判断二级标题
            if (data[i].t2Name === data[i - 1].t2Name) {
              this.spanArr2[this.pos2] += 1;
              this.spanArr2.push(0);
              // 判断三级标题
              if (data[i].t3Name === data[i - 1].t3Name) {
                this.spanArr3[this.pos3] += 1;
                this.spanArr3.push(0);
              }else{
                this.spanArr3.push(1);
                this.pos3 = i
              }
            }else{
              this.spanArr2.push(1);
              this.spanArr3.push(1);
              this.pos2 = i
              this.pos3 = i
            }
          }else{
            this.spanArr1.push(1);
            this.spanArr2.push(1);
            this.spanArr3.push(1);
            this.pos1 = i
            this.pos2 = i
            this.pos3 = i
          }
        }
      }
    },
    // 合并单元格
    objectSpanMethod({
      rowIndex,
      columnIndex
    }) {
      if (columnIndex === 0) {
        const _row = this.spanArr1[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 1) {
        const _row = this.spanArr2[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 2) {
        const _row = this.spanArr3[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        // console.log(`rowspan:${_row} colspan:${_col}`)
        return { // [0,0] 表示这一行不显示, [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        }
      }
    }
  }
}
</script>

不要删除

小站不易,感谢支持!