vue+el-table实现合并单元格

2022-04-15 0 875

本文实例为大家分享了el-table实现合并单元格的具体代码,供大家参考,具体内容如下

el-table合并单元格(vue+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.parentMerchantName == 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>
          <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实现合并单元格

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript vue+el-table实现合并单元格 https://www.niceym.com/25205.html