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>
相关推荐
酒尘&16 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
用户479492835691518 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户479492835691518 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v19 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
蓝瑟1 天前
告别重复造轮子!业务组件多场景复用实战指南
前端·javascript·设计模式
渴望成为python大神的前端小菜鸟1 天前
浏览器及其他 面试题
前端·javascript·ajax·面试题·浏览器
1024肥宅1 天前
手写 new 操作符和 instanceof:深入理解 JavaScript 对象创建与原型链检测
前端·javascript·ecmascript 6
soda_yo1 天前
浅拷贝与深拷贝: 克隆一只哈基米
前端·javascript·面试
用户6600676685391 天前
从“养猫”看懂JS面向对象:原型链与Class本质拆解
前端·javascript·面试
之恒君1 天前
JavaScript 对象相等性判断详解
前端·javascript