Vue+Element-UI el-table表格根据指定条件动态合并行span-method

当涉及到展示复杂数据的表格时,Element UI 中的 el-table 是一个非常有用的组件。在某些情况下,可能需要对表格进行合并显示以提高可读性。

案例需求:页面中有个表格组件,其中包含了一些学生的姓名、年级、负责班级和科目成绩等信息。我们希望能够合并显示相同学生的信息,并且在第一列(姓名)和第二列(负责年级)中进行合并操作。

1.处理后端返回的数据为表格所需要的数据

后端根据业务返回的数据:

此时我们需要将classList里的数据拉平放入新数组里,并且需要给每一份数据第一个要合并的数据做个标识,标明要合并几行

javascript 复制代码
 this.tableData = []
      res.data.forEach(item => {
        if (item.classList) {
          item.classList.forEach((item2, index) => {
            this.tableData.push({
              rowspan: index === 0 ? item.classList.length : null, // 需要合并的行数 只在合并首行也就是index==0时定义标识
              colspan: 1,
              ...item,
              ...item2
            })
          })
        }
      })

2.利用el-table的属性spanMethod去定义合并方法

  1. el-table标签上定义span-method方法
html 复制代码
  <el-table
    :data="tableData"
    :span-method="spanMethod">
  1. methods里增加spanMethod方法
javascript 复制代码
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if ([0, 1].includes(columnIndex)) {
        if (row.rowspan && row.rowspan > 0) {
          return {
            rowspan: row.rowspan, // 需要合并的行数
            colspan: 1
          }
        } else {
          // 不合并
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }

3.完整代码:

javascript 复制代码
<template>
  <el-table
    :data="tableData"
    :span-method="spanMethod"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="grade"
      label="负责年级">
    </el-table-column>
    <el-table-column
      prop="class"
      label="负责班级">
    </el-table-column>
    <el-table-column
      prop="sub1"
      label="科目1">
    </el-table-column>
    <el-table-column
      prop="score1"
      label="班级总分1">
    </el-table-column>
    <el-table-column
      prop="sub2"
      label="科目2">
    </el-table-column>
    <el-table-column
      prop="score2"
      label="班级总分2">
    </el-table-column>
    <el-table-column
      prop="sub3"
      label="科目3">
    </el-table-column>
    <el-table-column
      prop="score3"
      label="班级总分3">
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      // 这里模拟后端返回的数据 
      let res = {
        data: [
          {
            id: 1,
            name: '张三',
            grade: '一年级',
            classList: [
              {
                class: '1班',
                sub1: '语文',
                score1: '90',
                sub2: '数学',
                score2: '90',
                sub3: '英语',
                score3: '90'
              },
              {
                class: '2班',
                sub1: '物理',
                score1: '90',
                sub2: '化学',
                score2: '90',
                sub3: '英语',
                score3: '90'
              }
            ]
          }, {
            id: 2,
            name: '李四',
            grade: '二年级',
            classList: [
              {
                class: '1班',
                sub1: '语文',
                score1: '90',
                sub2: '数学',
                score2: '90',
                sub3: '英语',
                score3: '90'
              },
              {
                class: '2班',
                sub1: '-',
                score1: '-',
                sub2: '化学',
                score2: '90',
                sub3: '英语',
                score3: '90'
              }
            ]
          },
          {
            id: 3,
            name: '李四',
            grade: '二年级',
            classList: [
              {
                class: '1班',
                sub1: '语文',
                score1: '90',
                sub2: '数学',
                score2: '90',
                sub3: '英语',
                score3: '90'
              }
            ]
          }
        ]
      }
      this.tableData = []
      res.data.forEach(item => {
        if (item.classList) {
          item.classList.forEach((item2, index) => {
            this.tableData.push({
              rowspan: index === 0 ? item.classList.length : null, // 需要合并的行数 只在合并首行也就是index==0时定义标识
              colspan: 1,
              ...item,
              ...item2
            })
          })
        }
      })
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if ([0, 1].includes(columnIndex)) {
        if (row.rowspan && row.rowspan > 0) {
          return {
            rowspan: row.rowspan, // 需要合并的行数
            colspan: 1
          }
        } else {
          // 不合并
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
相关推荐
~无忧花开~5 分钟前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
cj814024 分钟前
Prompt,Agent,Skill,Mcp分别于langchain有什么关系
前端
SuperEugene38 分钟前
Axios + Vue 错误处理规范:中后台项目实战,统一捕获系统 / 业务 / 接口异常|API 与异步请求规范篇
前端·javascript·vue.js·前端框架·axios
行走的陀螺仪38 分钟前
手写 Vue3 极简 i18n
前端·javascript·vue.js·国际化·i18n
羽沢311 小时前
一篇简单的STOMP教程QAQ
前端·javascript·stomp
code_Bo1 小时前
使用AI完成Swagger接口类型在前端自动生成的工具
前端·后端·架构
加个鸡腿儿1 小时前
从"包裹器"到"确认按钮"——一个组件的三次重构
前端·vue.js·设计模式
子兮曰1 小时前
AI写代码坑了90%程序员!这5个致命bug,上线就炸(附避坑清单)
前端·javascript·后端
猪八宅百炼成仙1 小时前
PanelSplitter 组件:前端左右布局宽度调整的实用解决方案
前端
BUG胡汉三1 小时前
自建在线文档编辑服务:基于 Collabora CODE + Spring Boot + Vue 3 的完整实现
vue.js·spring boot·后端·在线编辑