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;
    },
相关推荐
陈随易几秒前
VSCode v1.102发布,AI体验大幅提升
前端·后端·程序员
markyankee1012 分钟前
Vue 表单输入绑定终极指南:从基础到企业级实践
vue.js
ma775 分钟前
JavaScript 获取短链接原始地址的解决方案
前端
该用户已不存在5 分钟前
关于我把Mac Mini托管到机房,后续来了,还有更多玩法
服务器·前端·mac
借月7 分钟前
🎯 用 Vue + SVG 实现一个「蛇形时间轴」组件,打造高颜值事件流程图
vue.js
tianchang8 分钟前
SSR 深度解析:从原理到实践的完整指南
前端·vue.js·设计模式
闲蛋小超人笑嘻嘻9 分钟前
前端面试十一之TS
前端
摆烂为不摆烂10 分钟前
😁深入JS(四): 一文让你完全了解Iterator+Generator 实现async await
前端
DoraBigHead22 分钟前
🧠 别急着传!大文件上传里,藏着 Promise 的高级用法
前端·javascript·面试
嘉琪00125 分钟前
封装一个有最小化的dialog组件
前端·javascript·css