ElementUI 用span-method实现循环el-table组件的合并行功能

需要把指定列的相同数据合并起来(项目中用的是updateTime)

后端返回的数据格式:

html:

html 复制代码
<el-tab-pane label="执行记录概览" name="fourth" v-loading="loading">
          <el-timeline v-if="recordList.length > 0">
            <el-timeline-item
              v-for="(item, index) in recordList"
              :key="index"
              :timestamp="item.createTime"
              placement="top"
            >
              <el-card>
                <el-tabs
                  v-model="activeName2[index]"
                  @tab-click="handleClick2($event, index)"
                >
                  <el-tab-pane label="阻断列表" name="first">
                    <el-table
                      :data="item.disposesList"
                      :span-method="(params) => objectSpanMethod(params, item)"
                      border
                    >
                      <!-- <el-table-column
                        align="center"
                        type="index"
                        label="序号"
                      /> -->
                      <el-table-column
                        prop="updateTime"
                        label="时间"
                        width="160"
                      >
                      </el-table-column>
                      <el-table-column prop="hostnameIp" label="域名/IP">
                      </el-table-column>
                      <!-- <el-table-column prop="type" label="阻断状态" width="100">
                        <template slot-scope="scope">
                          <span>{{ getBlockState(scope.row.blockState) }}</span>
                        </template>
                      </el-table-column> -->
                      <el-table-column prop="type" label="类型" width="100">
                        <template slot-scope="scope">
                          <span>{{ getTypelVal(scope.row.type) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="label" label="标签" width="120">
                        <template slot-scope="scope">
                          <span>{{ getLabelVal(scope.row.label) }}</span>
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-tab-pane>
                  <el-tab-pane label="快照截图" name="second">
                    <Snapshot :snapshotList="item.snapshotList"></Snapshot>
                  </el-tab-pane>
                  <el-tab-pane label="通信流量" name="third">
                    <Traffic :networkList="item.networkList"></Traffic>
                  </el-tab-pane>
                </el-tabs>
              </el-card>
            </el-timeline-item>
          </el-timeline>
          <div v-if="!recordList.length" class="nodata">
            <img class="empty-pic" src="@/assets/images/nodata.png" alt="" />
          </div>
        </el-tab-pane>

js:

javascript 复制代码
 data() {
    return {
         recordList: [],
          activeName2: {}, // 用一个对象来存储每个tab的激活状态
    }
 }

methods: {
handleClick2(tab, index) {
      this.$set(this.activeName2, index, tab.name);
 },
getAllList() {
      this.loading = true;
      getAllList({
        taskId: this.taskId,
      }).then((response) => {
        this.recordList = response.rows;
        this.loading = false;

        // 初始化activeTab对象
        this.recordList.forEach((item, index) => {
          this.$set(this.activeName2, index, "first"); // 假设默认第一个面板是激活的
        });
      });
  },
objectSpanMethod({ row, column, rowIndex, columnIndex }, item) {
      // console.log(row, column, rowIndex, columnIndex);
      if (columnIndex === 0) {
        // name列
        // 获取当前行的name值
        let currentName = row.updateTime;
        let previousName =
          rowIndex > 0 ? item.disposesList[rowIndex - 1].updateTime : null;
        let nextName =
          rowIndex < item.disposesList.length - 1
            ? item.disposesList[rowIndex + 1].updateTime
            : null;
        // 如果当前行的name与上一行相同,隐藏该单元格
        if (currentName === previousName) {
          return { rowspan: 0, colspan: 0 };
        }
        // 如果当前行的name与下一行相同,合并行
        let rowspan = 1;
        while (nextName === currentName) {
          rowspan++;
          rowIndex++;
          nextName =
            rowIndex < item.disposesList.length - 1
              ? item.disposesList[rowIndex + 1].updateTime
              : null;
        }
        return { rowspan, colspan: 1 };
      }
    },
},
相关推荐
_风满楼1 小时前
HTTP 请求的五种传参方式
前端·javascript·后端
光影少年2 小时前
前端线上屏幕出现卡顿如何排查?
开发语言·前端·javascript·学习·前端框架·node.js
像我这样帅的人丶你还2 小时前
前端监控体系与实践:从错误上报到内存与 GC 观测
前端·javascript·架构
a1117762 小时前
高斯泼溅 (Gaussian Splatting) 的 Three.js 实现
开发语言·javascript·ecmascript
代码北人生2 小时前
agent时代,我们都低估了这个 23k Star 的 Claude Code Skills 项目!
javascript
成都渲染101云渲染66662 小时前
云渲染全面支持3dsMax 2027,高效渲染体验升级
开发语言·前端·javascript
zs宝来了2 小时前
微前端架构:qiankun 沙箱隔离与样式冲突
前端·javascript·框架
M ? A3 小时前
Vue 的 scoped 样式穿透 React 不支持?用 VuReact 编译就行
前端·javascript·vue.js·react.js·面试·开源·vureact
zs宝来了3 小时前
Vue 3 Composition API:响应式系统与依赖追踪
前端·javascript·框架
村上小树3 小时前
非常简单地学习一下slate.js的原理
前端·javascript