el-table :span-method合并单元格

复制代码
          <el-table :data="tableData" :span-method="objectSpanMethod" border style="width: 100%" :height="300">
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column prop="projectType" align="center" width="120" label="指标分类"></el-table-column>
            <el-table-column prop="project" align="center" width="120" label="指标项目"></el-table-column>
            <el-table-column prop="indicator" align="center" label="标准分"></el-table-column>
            <el-table-column prop="score" align="center" label="得分数"></el-table-column>
          </el-table>
javascript 复制代码
data() {
    return {
        tableData: [],
        merge: [], // 存放需要合并的行
        subscript: '' // 需要合并行下标
    }
}

methods: {
    // 拿到数据
    getTableList() {
      this.$request({ url: '/fault/CmRaterecordService/factoryStationRat', method: 'get', params: this.queryParams }).then(res => {
        console.log(res)
        if (res?.data?.length) {
          this.tableData = res.data
          this.getMergeSubSceipt(res.data)
        }
      })
    },


    // 根据字段projectType做合并区分处理,具体看接口的区分字段
    getMergeSubSceipt(data) {
      if (data) {
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            this.merge.push(1)
            this.subscript = 0
          } else {
            // 判断当前元素与上一个元素是否相同
            // 根据相同id进行合并,根据需要可进行修改
            if (data[i].projectType === data[i - 1].projectType) {
              this.merge[this.subscript] += 1
              this.merge.push(0)
            } else {
              this.merge.push(1)
              this.subscript = i
            }
          }
        }
      }
    },
    
    // 合并处理
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        const _row = this.merge[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
}

效果:

附:通过接口获取的tableData数据结构如下

相关推荐
1024肥宅5 小时前
JavaScript 拷贝全解析:从浅拷贝到深拷贝的完整指南
前端·javascript·ecmascript 6
欧阳天风5 小时前
js实现鼠标横向滚动
开发语言·前端·javascript
局i5 小时前
Vue 指令详解:v-for、v-if、v-show 与 {{}} 的妙用
前端·javascript·vue.js
꒰ঌ小武໒꒱6 小时前
RuoYi-Vue 前端环境搭建与部署完整教程
前端·javascript·vue.js·nginx
局i8 小时前
Vue 中 v-text 与 v-html 的区别:文本渲染与 HTML 解析的抉择
前端·javascript·vue.js
+VX:Fegn08958 小时前
计算机毕业设计|基于springboot+vue的学校课程管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
fruge8 小时前
接口 Mock 工具对比:Mock.js、Easy Mock、Apifox 的使用场景与配置
开发语言·javascript·ecmascript
贩卖黄昏的熊8 小时前
typescript 快速入门
开发语言·前端·javascript·typescript·ecmascript·es6
一 乐9 小时前
水果销售|基于springboot + vue水果商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
JIngJaneIL9 小时前
校园任务平台|校园社区系统|基于java+vue的校园悬赏任务平台系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园任务平台