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数据结构如下

相关推荐
PleaSure乐事12 分钟前
【Node.js】内置模块FileSystem的保姆级入门讲解
javascript·node.js·es6·filesystem
雷特IT22 分钟前
Uncaught TypeError: 0 is not a function的解决方法
前端·javascript
.生产的驴1 小时前
Electron Vue框架环境搭建 Vue3环境搭建
java·前端·vue.js·spring boot·后端·electron·ecmascript
awonw1 小时前
[前端][easyui]easyui select 默认值
前端·javascript·easyui
老齐谈电商1 小时前
Electron桌面应用打包现有的vue项目
javascript·vue.js·electron
LIURUOYU4213081 小时前
vue.js组建开发
vue.js
九圣残炎1 小时前
【Vue】vue-admin-template项目搭建
前端·vue.js·arcgis
《源码好优多》2 小时前
基于SpringBoot+Vue+Uniapp的植物园管理小程序系统(2024最新,源码+文档+远程部署+讲解视频等)
vue.js·spring boot·uni-app
柏箱2 小时前
使用JavaScript写一个网页端的四则运算器
前端·javascript·css
计算机学姐2 小时前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis