element-UI动态渲染表格时相同数据表格合并,比如日期等数据相同的地方合并单元格,具体看代码

    <el-table :data="tableDataArr" :span-method="cellMerge" style="width: 100%;display:inline-block;">
          <el-table-column prop="stat_date" sortable label="日期" width="120"></el-table-column>
        <el-table-column  prop="request_uri" label="URL" width="700"></el-table-column>
        <el-table-column prop="total" sortable label="total" width="320"></el-table-column>
      </el-table>



data():{
return{
  tableDataArr: [],//存放表格数据
      spanArr: [],//二维数组,用于存放单元格合并规则
      position: 0,//用于存储相同项的开始index
}
}
methods:{
getData(){
this.rowspan(0,'stat_date');
this.rowspan(1,'request_uri'); 
this.rowspan(2,'total');
}
rowspan(idx, prop) {
      this.spanArr[idx] = [];
      this.position = 0; 
      this.tableDataArr.forEach((item,index) => {
        if( index === 0){
          this.spanArr[idx].push(1);
          this.position = 0;
        }else{
          if(this.tableDataArr[index][prop] === this.tableDataArr[index-1][prop] ){
            this.spanArr[idx][this.position] += 1;//有相同项
            this.spanArr[idx].push(0); // 名称相同后往数组里面加一项0
          }else{
            this.spanArr[idx].push(1);//同列的前后两行单元格不相同
            this.position = index;
          }
        }
      })
    },
    //表格单元格合并
    cellMerge({ row, column, rowIndex, columnIndex }) {
      for(let i = 0; i<this.spanArr.length; i++) {
        if(columnIndex === i){
          const _row = this.spanArr[i][rowIndex];
          const _col = _row>0 ? 1 : 0;
          // console.log('第'+rowIndex+'行','第'+i+'列','rowspan:'+_row,'colspan:'+_col)
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      }
    }
}

  

posted @ 2020-10-30 10:43  慕一生,晓一世  阅读(374)  评论(0编辑  收藏  举报