1. 下面图是实现效果

2. 代码实现

        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>

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

 2、js代码

// 获取合并的数组
    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();!!!

 如有不会的内容. 请在下面留言

Logo

前往低代码交流专区

更多推荐