element-ui的el-table表格合并列

一、需求描述,如图,相同的区域位置进行合并,前提:后台给的数据相同区域名称的要放在临近。

element-ui的el-table表格合并列_第1张图片

二、代码实现

1、html页面


      
      
        
      
    

2、js代码

data中定义position: 0,rowSpanArr: [],

// 获取合并的数组
    getRowSpan() {
      this.rowSpanArr = [];
      this.tableData.forEach((item, index) => {
        if (index == 0) {
          this.rowSpanArr.push(1);
          this.position = 0;
        } else {
          if (this.tableData[index].deviceLocation == this.tableData[index - 1].deviceLocation) {
            this.rowSpanArr[this.position] += 1; //项目名称相同,合并到同一个数组中
            this.rowSpanArr.push(0);
            this.tableData[index].deviceLocation = this.tableData[index - 1].deviceLocation;
          } else {
            this.rowSpanArr.push(1);
            this.position = index;
          }
        }
      });
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 只合并区域位置
      if (columnIndex === 0) {
        const _row = this.rowSpanArr[rowIndex];
        return {
          rowspan: _row, //行
          colspan: 1 //列
        };
      }
    }

注意:列表数据获取完毕之后,调用this.getRowSpan();!!!

你可能感兴趣的:(Vue)