element-ui中table函数合并单元格

<el-table :data="tableData"
                  style="width:100%;"
                  :height="tableHeight"
                  border
                  :span-method="objSpanMethod"></el-table>

在这里插入图片描述

mergeLineMethod () {
     //处理合并表格数据
      let mergeLine = this.tableData
      mergeLine.forEach((res, i) => {
     
        if (i === 0) {
     
          // 二
          this.mergeLineArrSec.push(1);
          this.mergeLineIndexSec = 0
          // 一
          this.mergeLineArrFir.push(1);
          this.mergeLineIndexFir = 0
        } else {
     
          // 判断当前元素与上一个元素是否相同,是就给之前相同的第一个+1,并且数组添加一个0
          // 判断第二列的数据相同
          if (mergeLine[i].firClass === mergeLine[i - 1].firClass) {
     
            if (mergeLine[i].secClass === mergeLine[i - 1].secClass) {
     
              this.mergeLineArrSec[this.mergeLineIndexSec] += 1;
              this.mergeLineArrSec.push(0);
            } else {
     //如果与前一个不相同,则追加一个新从1开始的数字,此时更新mergeLineIndex的值
              this.mergeLineArrSec.push(1);
              this.mergeLineIndexSec = i;
            }
          } else {
     
            this.mergeLineArrSec.push(1);
            this.mergeLineIndexSec = i;
          }
          // 判断第一列的数据相同
          if (mergeLine[i].firClass === mergeLine[i - 1].firClass) {
     
            this.mergeLineArrFir[this.mergeLineIndexFir] += 1;
            this.mergeLineArrFir.push(0);
          } else {
     //如果与前一个不相同,则追加一个新从1开始的数字,此时更新mergeLineIndex的值
            this.mergeLineArrFir.push(1);
            this.mergeLineIndexFir = i;
          }
        }
      });
    },
// 合并单元格
    objSpanMethod ({
     
      row,
      column,
      rowIndex,
      columnIndex
    }) {
     
      //  * row 表格每一行的数据
      //  * column 表格每一列的数据
      //  * rowIndex 表格的行索引,不包括表头,从0开始
      //  * columnIndex 表格的列索引,从0开始
      if (columnIndex === 0 || columnIndex === 1) {
     
        if (columnIndex === 0) {
     
          const _row = this.mergeLineArrFir[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
     
            rowspan: _row,
            colspan: _col
          }
        } else if (columnIndex === 1) {
     
          const _row = this.mergeLineArrSec[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
     
            rowspan: _row,
            colspan: _col
          }
        }
      }
    },

效果是 把前两列的所有相同的一级标题进行合并,在一级标题的基础上在把二级标题响应的合并。
element-ui中table函数合并单元格_第1张图片

你可能感兴趣的:(vue,oa系统,vue,js)