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 小时前
Chrome 扩展如何操作浏览器:chrome.debugger 与 CDP 的区别
javascript
狮子座的男孩2 小时前
javascript 基础 es6:1、let 声明,let 实践案例,const 声明,解构赋值
前端·javascript·经验分享·es6·const声明·解构赋值·let声明及案例
KevinWang_3 小时前
iOS 开发计划(二) - JavaScript 基本语法
javascript
KevinWang_3 小时前
iOS 开发计划(一) - 通过 Scriptable app 在 iOS 上运行 JavaScript 脚本
javascript·ios
htzyl2063 小时前
async/await 和 Promise 到底怎么选?
javascript·promise·ecmascript 6
WJ.Polar4 小时前
JavaScript入门
前端·javascript
夏与冰6 小时前
Vue2项目中数据字典的使用封装
前端·javascript·vue.js·elementui
map_vis_3d6 小时前
百度地图 Hello World:5 分钟用 JSAPI 4.0 显示一张地图
前端·javascript·地图·百度地图
kyriewen7 小时前
我把 7 个 UI 库的下拉框用键盘敲了一遍,3 家出了岔子
前端·javascript·程序员
tryCbest8 小时前
Vue系列——第 24 篇:VueUse——组合式工具集的百宝箱
前端·vue.js