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;
    },
相关推荐
Uyker1 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder5 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客6 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro7 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom7 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...7 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡8 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜059 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx10 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing99910 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序