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

        <el-table :data="tableDataArr" :span-method="cellMerge" style=" 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
              }
            }
          }
        }
    }

      

  • 相关阅读:
    Add Two Numbers
    Same Tree
    Single Number
    题目1190:大整数排序
    题目1182:统计单词
    题目1181:遍历链表
    题目1180:对称矩阵
    题目1179:阶乘
    题目1206:字符串连接
    HTML事件
  • 原文地址:https://www.cnblogs.com/bigkuan/p/13900942.html
Copyright © 2011-2022 走看看