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>
相关推荐
β添砖java9 分钟前
案例二:登高千古第一绝句
前端·javascript·css
TNTLWT15 分钟前
单例模式(C++)
javascript·c++·单例模式
落日沉溺于海1 小时前
React From表单使用Formik和yup进行校验
开发语言·前端·javascript
知识分享小能手1 小时前
React学习教程,从入门到精通, React 新创建组件语法知识点及案例代码(11)
前端·javascript·学习·react.js·架构·前端框架·react
an__ya__1 小时前
Vue数据响应式reactive
前端·javascript·vue.js
华仔啊1 小时前
面试都被问懵了?CSS 的 flex:1 和 flex:auto 真不是一回事!90%的人都搞错了
前端·javascript
前端康师傅1 小时前
JavaScript 函数详解
前端·javascript
葡萄城技术团队1 小时前
从基础到实战:一文吃透 JS Tuples 与 Records 的所有核心用法
javascript
@小红花2 小时前
从0到1学习Vue框架Day03
前端·javascript·vue.js·学习·ecmascript
前端与小赵2 小时前
vue3中 ref() 和 reactive() 的区别
前端·javascript·vue.js