elementui+vue 多行数据的合并单元格

多行的合并,可以参考,改改就能用

html

html 复制代码
<template>
	<el-table :data="students" :span-method="objectSpanMethod">
		<el-table-column prop="grade" label="年级"></el-table-column>
		<el-table-column prop="class" label="班级"></el-table-column>
		<el-table-column prop="name" label="姓名"></el-table-column>
		<el-table-column prop="score" label="分数"></el-table-column>
	</el-table>
</template>

数据

javascript 复制代码
 students: 
[
      { grade: '一年级', class: '一班', name: '张三', score: 85 },
      { grade: '一年级', class: '一班', name: '张三', score: 90 },
      { grade: '一年级', class: '一班', name: '李四', score: 78 },
      { grade: '一年级', class: '一班', name: '李四', score: 82 },
      { grade: '一年级', class: '二班', name: '王五', score: 92 },
      { grade: '一年级', class: '二班', name: '王五', score: 88 },
      { grade: '一年级', class: '二班', name: '王五', score: 95 },
      { grade: '二年级', class: '一班', name: '赵六', score: 80 },
      { grade: '二年级', class: '一班', name: '赵六', score: 85 },
      { grade: '二年级', class: '一班', name: '孙七', score: 76 },
      { grade: '二年级', class: '一班', name: '孙七', score: 81 },
      { grade: '三年级', class: '一班', name: '周八', score: 83 },
      { grade: '三年级', class: '一班', name: '周八', score: 87 }
    ]

js代码,处理数据,合并单元格

javascript 复制代码
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) { 
        // 年级列
        if (rowIndex === 0 || this.students[rowIndex - 1].grade !== row.grade) {
          return {
            rowspan: this.getRowspanByGrade(row.grade),
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      } else if (columnIndex === 1) { 
        // 班级列
        if (rowIndex === 0 || this.students[rowIndex - 1].grade !== row.grade || this.students[rowIndex - 1].class !== row.class) {
          return {
            rowspan: this.getRowspanByClass(row.class, row.grade),
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      } else if (columnIndex === 2) { 
        // 姓名列
        if (rowIndex === 0 || this.students[rowIndex - 1].grade !== row.grade || this.students[rowIndex - 1].class !== row.class || this.students[rowIndex - 1].name !== row.name) {
          return {
            rowspan: this.getRowspanByName(row.name, row.grade, row.class),
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    },
    // 获取对应的数组长度
    getRowspanByGrade(grade) {
      return this.students.filter(student => student.grade === grade).length;
    },
    getRowspanByClass(classname, grade) {
      return this.students.filter(student => student.class === classname && student.grade === grade).length;
    },
    getRowspanByName(name, grade, classname) {
      return this.students.filter(student => student.name === name && student.grade === grade && student.class === classname).length;
    },
相关推荐
OEC小胖胖4 小时前
去中心化身份:2025年Web3身份验证系统开发实践
前端·web3·去中心化·区块链
vvilkim5 小时前
Electron 进程间通信(IPC)深度优化指南
前端·javascript·electron
ai小鬼头7 小时前
百度秒搭发布:无代码编程如何让普通人轻松打造AI应用?
前端·后端·github
漂流瓶jz7 小时前
清除浮动/避开margin折叠:前端CSS中BFC的特点与限制
前端·css·面试
前端 贾公子7 小时前
在移动端使用 Tailwind CSS (uniapp)
前端·uni-app
散步去海边7 小时前
Cursor 进阶使用教程
前端·ai编程·cursor
清幽竹客7 小时前
vue-30(理解 Nuxt.js 目录结构)
前端·javascript·vue.js
知性的小mahua7 小时前
echarts+vue实现中国地图板块渲染+省市区跳转渲染
vue.js
weiweiweb8887 小时前
cesium加载Draco几何压缩数据
前端·javascript·vue.js
幼儿园技术家7 小时前
微信小店与微信小程序简单集成指南
前端