element-ui的el-table表格合并列

一、需求描述,如图,相同的区域位置进行合并,前提:后台给的数据相同区域名称的要放在临近。

二、代码实现

1、html页面

<el-table :data="tableData"
              border
              size="mini"
              align="center"
              :span-method="objectSpanMethod">
      <el-table-column prop="deviceLocation"
                       label="区域名称"
                       align="center"></el-table-column>
      <el-table-column prop="deviceName"
                       label="设备名称"
                       align="center"></el-table-column
      <el-table-column prop="hour23"
                       label="23"
                       width="40"
                       align="center">
        <template slot-scope="scope">
          <span class="my-span"
                v-if="scope.row.hour23 == 1">√</span>
          <span class="my-span"
                v-if="scope.row.hour23 == 2">○</span>
          <span class="my-span"
                v-if="scope.row.hour23 == 3">△</span>
          <span class="my-span"
                v-if="scope.row.hour23 == 4">×</span>
          <span v-if="scope.row.hour23 == -1">—</span>
        </template>
      </el-table-column>
    </el-table>

2、js代码

data中定义position: 0,rowSpanArr: [],

// 获取合并的数组
    getRowSpan() {
      this.rowSpanArr = [];
      this.tableData.forEach((item, index) => {
        if (index == 0) {
          this.rowSpanArr.push(1);
          this.position = 0;
        } else {
          if (this.tableData[index].deviceLocation == this.tableData[index - 1].deviceLocation) {
            this.rowSpanArr[this.position] += 1; //项目名称相同,合并到同一个数组中
            this.rowSpanArr.push(0);
            this.tableData[index].deviceLocation = this.tableData[index - 1].deviceLocation;
          } else {
            this.rowSpanArr.push(1);
            this.position = index;
          }
        }
      });
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 只合并区域位置
      if (columnIndex === 0) {
        const _row = this.rowSpanArr[rowIndex];
        return {
          rowspan: _row, //行
          colspan: 1 //列
        };
      }
    }

注意:列表数据获取完毕之后,调用this.getRowSpan();!!!

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值