element-ui table表格动态列兼并

媒介

element-ui 1.0的时刻没有表格兼并的要领,这当时做表格兼并的功用时刻,异常头疼。2.0最先官方已支撑表格兼并的要领,那怎样做到表格动态兼并呢,照样直接看代码吧。

官方例子

经由过程给table传入span-method要领能够完成兼并行或列,要领的参数是一个对象,内里包括当前行row、当前线column、当前行号rowIndex、当前线号columnIndex四个属性。该函数能够返回一个包括两个元素的数组,第一个元素代表rowspan,第二个元素代表colspan。 也能够返回一个键名为rowspan和colspan的对象。

<template>
  <div>
    <el-table
      :data="tableData6"
      :span-method="objectSpanMethod"
      border
      style="width: 100%; margin-top: 20px">
      <el-table-column
        prop="id"
        label="ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="amount1"
        label="数值 1(元)">
      </el-table-column>
      <el-table-column
        prop="amount2"
        label="数值 2(元)">
      </el-table-column>
      <el-table-column
        prop="amount3"
        label="数值 3(元)">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData6: [{
          id: '12987122',
          name: '王小虎',
          amount1: '234',
          amount2: '3.2',
          amount3: 10
        }, {
          id: '12987123',
          name: '王小虎',
          amount1: '165',
          amount2: '4.43',
          amount3: 12
        }, {
          id: '12987124',
          name: '王小虎',
          amount1: '324',
          amount2: '1.9',
          amount3: 9
        }, {
          id: '12987125',
          name: '王小虎',
          amount1: '621',
          amount2: '2.2',
          amount3: 17
        }, {
          id: '12987126',
          name: '王小虎',
          amount1: '539',
          amount2: '4.1',
          amount3: 15
        }]
      };
    },
    methods: {
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (columnIndex === 0) {
          if (rowIndex % 2 === 0) {
            return {
              rowspan: 2,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }
      }
    }
  };
</script>

https://jsfiddle.net/ve4sy51x/1/

兼并要领

https://jsfiddle.net/ve4sy51x…
经由过程一层一层往下找,

 objectSpanMethod({ row, column, rowIndex, columnIndex }) {
         if(cacheData.length<=0){
           return false
         }
          let colNum = cacheData[rowIndex][columnIndex];
          if (colNum < 2) {
            return {
              rowspan: colNum,
              colspan: colNum
            }
          } else {
          return {
            rowspan: colNum,
            colspan: 1
          }
        }
      },
      combine(){
        this.tableData.forEach((res,i)=> {
          cacheData[i] = [];
          colData.forEach((item, j) => {
            if (i === 0) {
              cacheData[0][j] = 1;
              cache = JSON.parse(JSON.stringify(res));
              cacheIndex[j] = 0;
            } else {
              if(res[item.prop] === cache[item.prop] && item.prop !=='index'){
                cacheData[cacheIndex[j]][j]++;
                cacheData[i][j] = 0
              }else{
                cache[item.prop] = res[item.prop];
                cacheIndex[j] = i;
                cacheData[i][j] = 1;
              }
            }
          })
        })    
      }

预处理天生一个二维数组,然后在放在objectSpanMethod要领内里。

    原文作者:anick
    原文地址: https://segmentfault.com/a/1190000015841321
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞