Element Table 合并列

 <el-table
        :data="tableInfo.tableData"
        stripe
        height="calc(100% - 32px)"
        fit
        :span-method="rowspanMethod"
        :cell-style="cellstyle"
        @sort-change="sortchange"
      >
  <template v-for="(col,index) in tableInfo.columns">
    <el-table-column
            :prop="col.prop"
            :key="index"
            align="center"
            :label="col.label"
            :width="col.width||100"
          ></el-table-column>
  </template>
  </el-table>

 

     // 通用行合并函数(将相同多列数据合并为一行)
    rowspanMethod({ row, column, rowIndex, columnIndex }) {
      let tableData = this.tableData    // table的原始数据
      // 合并规则 column列名,condition:column合并的条件列,只有condition所有列相等才合并column列
      const mergeColumn = [
        { colmun: 'BDMC', condition: ['id', 'BDDM'] },
        { colmun: 'XMMC', condition: ['id', 'BDDM', 'XMDM'] },
        { colmun: 'BZMC', condition: ['id', 'BDDM', 'XMDM', 'BZDM'] },
        { colmun: 'KFMC', condition: ['id', 'BDDM', 'XMDM', 'BZDM', 'KFDM'] },
        { colmun: 'Jsrqc', condition: ['id'] }
      ]
      // 需要合并的列名
      const fields = this.mergeColumns.map((m) => m.colmun)
      
      // 比较函数,条件符合返回true,条件不成立返回false
      let compare = (pre, next, mergeItem) => {
        for (let i = 0; i < mergeItem.condition.length; i++) {
          const item = mergeItem.condition[i]
          if (pre[item] != next[item]) {
            return false
          }
        }
        return true
      }

      const cellValue = row[column.property]
      // cellValue为空时也进行比较
      if (fields.includes(column.property)) {
        const prevRow = tableData[rowIndex - 1]
        let nextRow = tableData[rowIndex + 1]
        let condition = mergeColumn.find((m) => m.colmun == column.property)
        if (prevRow && compare(prevRow, row, condition)) {
          return { rowspan: 0, colspan: 0 }
        } else {
          let countRowspan = 1
          while (nextRow && compare(row, nextRow, condition) && condition) {
            nextRow = tableData[++countRowspan + rowIndex]
          }
          if (countRowspan > 1) {
            return { rowspan: countRowspan, colspan: 1 }
          }
        }
      }
    }

 

posted @ 2020-09-29 14:12  a迪  阅读(2855)  评论(0编辑  收藏  举报