el-table 合并单元格_以合并属性值相同行为例

在做表格展示时,通常会遇到合并相同日期/id行的需求;

javascript 复制代码
<template>
  <div>
    <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 lang="ts" setup>
import type { TableColumnCtx } from "element-plus";

const tableData: User[] = [
  {
    id: "12987122",
    name: "Tom",
    amount1: "234",
    amount2: "3.2",
    amount3: 10,
  },
  {
    id: "12987122",
    name: "Tom",
    amount1: "234",
    amount2: "3.2",
    amount3: 10,
  },
  
  {
    id: "12987124",
    name: "Tom",
    amount1: "324",
    amount2: "1.9",
    amount3: 9,
  },
  {
    id: "12987125",
    name: "Tom",
    amount1: "621",
    amount2: "2.2",
    amount3: 17,
  },
  {
    id: "12987126",
    name: "Tom",
    amount1: "539",
    amount2: "4.1",
    amount3: 15,
  },
  {
    id: "12987123",
    name: "Tom2",
    amount1: "1565",
    amount2: "4.4453",
    amount3: 124,
  },
  {
    id: "12987123",
    name: "Tom3",
    amount1: "16523",
    amount2: "4.433",
    amount3: 123,
  },
  {
    id: "12987123",
    name: "Tom",
    amount1: "165",
    amount2: "4.43",
    amount3: 12,
  },
];
interface User {
  id: string;
  name: string;
  amount1: string;
  amount2: string;
  amount3: number;
}

interface SpanMethodProps {
  row: User;
  column: TableColumnCtx<User>;
  rowIndex: number;
  columnIndex: number;
}

const handleTableData = (tableData: any) => {
  let rowSpanArr: any = [],
    position = 0;
  tableData.forEach((item: any, index: number) => {
    if (index == 0) {
      rowSpanArr.push(1);
      position = 0;
    } else {
      if (item.id == tableData[index - 1].id) {
        rowSpanArr[position] += 1; //id相同,数组中的某一项值+1
        rowSpanArr.push(0);   // 被合并的行,push 0 占位
      } else {
        rowSpanArr.push(1);
        position = index;
      }
    }
  });
  return rowSpanArr;
};

const objectSpanMethod = ({
  row,
  column,
  rowIndex,
  columnIndex,
}: SpanMethodProps) => {
  let rowSpanArr = handleTableData(tableData);
  if (columnIndex === 0) {
    const rowSpan = rowSpanArr[rowIndex];
    return {
      rowspan: rowSpan, //行
      colspan: 1, //列
    };
  }
};
</script>
相关推荐
爱勇宝17 小时前
AI 时代,前端工程师的话语权正在下降?
前端·后端
kymjs张涛17 小时前
一个月,纯VibeCoding,全平台云笔记APP
前端·javascript·后端
巴勒个啦17 小时前
esbuild 插件实战:5个真实场景带你自定义构建流水线
前端·angular.js
英勇无比的消炎药17 小时前
一文吃透TinyRobot Bubble:从基础组件搭建完整AI消息渲染体系
vue.js
狗头大军之江苏分军17 小时前
前端路由是怎么来的
前端·javascript·后端
英勇无比的消炎药17 小时前
深挖底层:TinyRobot Bubble消息气泡组件核心技术原理
vue.js
英勇无比的消炎药17 小时前
架构剖析:TinyRobot Bubble渲染器状态管理与工具调用机制
vue.js
英勇无比的消炎药17 小时前
多模态消息渲染实战:TinyRobot Bubble内容解析与contentResolver用法
vue.js
Patrick_Wilson17 小时前
Cookie 作用域避坑:父域泄漏、同名优先级与多环境隔离
前端·http·浏览器
api工厂17 小时前
ZCode 3.0 版本搭配GLM-5.2能力测试
前端·人工智能·ai