element + table 行列合并

如图,实现通过判断数据,动态的合并列数据

复制代码
<template>
  <div class="merge-cell">
    <el-table
        :data="tableData"
        :span-method="objectSpanMethod"
        border
        style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="id" label="ID" width="180"/>
      <el-table-column prop="name" label="Name"/>
      <el-table-column prop="amount1" label="Amount 1"/>
      <el-table-column prop="amount2" label="Amount 2"/>
      <el-table-column prop="amount3" label="Amount 3"/>
    </el-table>
  </div>
</template>
<script setup lang="ts">
import type {TableColumnCtx} from 'element-plus';
interface User {
  id: string
  name: string
  amount1: string
  amount2: string
  amount3: number
}
interface SpanMethodProps {
  row: User
  column: TableColumnCtx<User>
  rowIndex: number
  columnIndex: number
}
const tableData: User[] = [
  {
    id: '12987122',
    name: 'Tom',
    amount1: '234',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '12987122',
    name: 'Tom',
    amount1: '165',
    amount2: '4.43',
    amount3: 12,
  },
  {
    id: '12987124',
    name: 'Tom',
    amount1: '324',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '12987125',
    name: 'Tom',
    amount1: '621',
    amount2: '2.2',
    amount3: 9,
  },
  {
    id: '12987126',
    name: 'Tom',
    amount1: '539',
    amount2: '4.1',
    amount3: 15,
  },
  {
    id: '129871212',
    name: 'Tom1',
    amount1: '539',
    amount2: '4.1',
    amount3: 151,
  },
  {
    id: '129871213',
    name: 'Tom2',
    amount1: '539',
    amount2: '4.1',
    amount3: 152,
  },
  {
    id: '129871213',
    name: 'Tom3',
    amount1: '539',
    amount2: '4.1',
    amount3: 152,
  }
]
const objectSpanMethod = ({
      row, // 行
      column, // 列
      rowIndex, // 行的下标
      columnIndex  // 列的下标
    }: SpanMethodProps) => {
  // 只判断第一列和第二列
  if (columnIndex === 0 || columnIndex === 1) {
    // 获取当前单元格的值
    const currentValue = (row as any)[column.property];

    // 获取上一行相同列的值
    const preRow: any = tableData[rowIndex - 1];
    const preValue = preRow ? preRow[column.property] : null;

    // 如果当前值和上一行的值相同,则将当前单元格隐藏
    if (currentValue === preValue) {
      return { rowspan: 0, colspan: 0 };
    } else {
      // 否则计算当前单元格应该跨越多少行
      let rowspan = 1;
      for (let i = rowIndex + 1; i < tableData.length; i++) {
        const nextRow: any = tableData[i];
        const nextValue = nextRow[column.property];
        if (nextValue === currentValue) {
          rowspan++;
        } else {
          break;
        }
      }
      return { rowspan, colspan: 1 };
    }
 }
}
</script>
相关推荐
OEC小胖胖6 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水6 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
老虎06277 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台7 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue
烛阴7 小时前
Babel 完全上手指南:从零开始解锁现代 JavaScript 开发的超能力!
前端·javascript
CN-Dust8 小时前
[FMZ][JS]第一个回测程序--让时间轴跑起来
javascript
全宝9 小时前
🎨前端实现文字渐变的三种方式
前端·javascript·css
yanlele9 小时前
前端面试第 75 期 - 2025.07.06 更新前端面试问题总结(12道题)
前端·javascript·面试
妮妮喔妮9 小时前
【无标题】
开发语言·前端·javascript
fie888910 小时前
浅谈几种js设计模式
开发语言·javascript·设计模式