1. 程式人生 > 程式設計 >vue+el-table實現合併單元格

vue+el-table實現合併單元格

本文例項為大家分享了el-table實現合併單元格的具體程式碼,供大家參考,具體內容如下

el-table合併單元格(+element)

- 先在el-table放入:span-method="arraySpanMethod"

<el-table :header-cell-style="{background:'#eef1f6',color:'#606266'}" :data="merchantList" border :span-method="arraySpanMethod">
          <el-table-column align="center" prop="provinceName"
label="省份"> </el-table-column> <el-table-column align="center" label="代理商名稱"> <template scope="scope"> <span>{{scope.row.parentMerchantNcAZtIeYame == scope.row.merchantName ? '---' : scope.row.parentMerchantName}}</span> </template> </el-table-column> 客棧
<el-table-column align="center" prop="cityName" label="市"> </el-table-column> <el-table-column align="center" prop="countryName" label="區"> </el-table-column> http://www.cppcns.com <el-table-column align="center" prop="merchantName" label="門店"> </el-table-column> </el-table>

在methods中寫入方法:

//合併單元格
arraySpanMethod ({ row,column,rowIndex,columnIndex }) {
      if (columnIndex === 0) {//第一列的合併方法,省
        const _row_1 = this.provinceArr[rowIndex];
        const _col_1 = _row_1 > 0 ? 1 : 0; //如果被合併了_row=0則它這個列需要取消
        return {
          rowspan: _row_1,colspan: _col_1
        }
      } 
    },//初始化
    merageInit () {
      this.provinceArr = []
      this.provincePos = 0
    },//要合併的陣列的方法
    merage () {
      this.merageInit()
      for (var i = 0; i < this.merchantList.length; i++) {
        if (i === 0) {
          //第一行必須存在
          this.provinceArr.push(1)
          this.provincePos = 0
        } else {
          // 判斷當前元素與上一個元素是否相同 this.provincePos是provinceArr內容的序號
          //省
          if (this.merchantList[i].provinceName === this.merchantList[i - 1].provinceName) {
            this.provinceArr[this.provincePos] += 1
            this.provinceArr.push(0)
          } else {
            this.provinceArr.push(1)
            this.provincePos = i
          }
        }
      }
    },

結果展示:

vue+el-table實現合併單元格

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。