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;
    },
相关推荐
1-1=07 小时前
Vue 全局事件总线
前端·javascript·vue.js
唐老板7 小时前
前端和后端,转 AI 全栈怎么转
前端·后端
程序员鱼皮8 小时前
Antigravity 保姆级实战教程,从 0 到 1 万字干货!
前端·后端·ai编程
徐小夕8 小时前
JitKnow AI知识库:把任何文档,一键变成可访问和可配置的Wiki站点
前端·vue.js·github
狮子座的男孩8 小时前
javascript 基础 es6:2、模板字符串,简化对象写法,箭头函数,箭头函数实践案例
前端·javascript·经验分享·es6·模板字符串·箭头函数·简化对象
夏与冰8 小时前
Vue2项目动态路由的实现思路
vue.js
陈如水9 小时前
AI Agent 主流架构
前端·javascript·react.js
Amos_Web9 小时前
通过 Rspack 源码学 Rust(七):Copy 与 Clone,什么时候复制,什么时候移动
前端
map_3d_vis9 小时前
异步加载百度地图:callback、Promise 封装和 jsapi-loader(JSAPI 4.0)
前端·javascript·性能优化·百度地图
用户47949283569159 小时前
开源突破 10 万+ Star:Archify v3.3.2 深度拆解——超越 Mermaid 的离线架构诊断、1080p Story 视频与 PR 变更可视化
前端·后端·agent