el-table合并单元格(vue+element)

这篇博客详细介绍了如何在Vue项目中结合Element UI库实现表格单元格的合并。通过在`el-table`组件中使用`:span-method`属性,并在methods中定义`arraySpanMethod`方法来动态计算合并行数和列数。博客内容包括设置表格样式、初始化合并数组以及处理数据逻辑,实现了根据数据内容自动合并相同省份的单元格,优化了表格展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
          }
        }
      }
    },
  • 结果展示:
    在这里插入图片描述
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值