ant-design表格自动合并相同内容的单元格

news/2024/9/18 23:13:56 标签: 表格

表格自动合并相同内容的单元格

合并hooks

import { TableColumnProps } from 'antd'

export const useAutoMergeTableCell = <T extends object>(
  dataSource: Array<T>,
  columns: Array<TableColumnProps> | Array<keyof T>
): Map<keyof T, Array<number>> => {
  const cellToIndexMap = new Map<keyof T, Array<number>>()
  const countInfoMap = new Map<keyof T, { startIndex: number; count: number; value: any } | null>()
  const length = dataSource.length

  const parseColumns = columns.map((m) => {
    if (typeof m === 'string') {
      return {
        dataIndex: m
      }
    }
    return m
  })

  parseColumns.forEach((it) => {
    cellToIndexMap.set(it.dataIndex as keyof T, new Array(length).fill(0))
    countInfoMap.set(it.dataIndex as keyof T, null)
  })

  const updateCellCount = (key: string) => {
    const countInfo = countInfoMap.get(key as keyof T)
    const indexArray = cellToIndexMap.get(key as keyof T)
    indexArray![countInfo!.startIndex] = countInfo!.count
  }

  const calculateCount = (key: string, value: any, index: number) => {
    const countInfo = countInfoMap.get(key as keyof T)
    if (countInfo === null) {
      countInfoMap.set(key as keyof T, { startIndex: index, count: 1, value })
    } else {
      if (countInfo!.value === value) {
        countInfo!.count++
      } else {
        updateCellCount(key)
        countInfoMap.set(key as keyof T, { startIndex: index, count: 1, value })
      }
    }

    if (index === length - 1) {
      updateCellCount(key)
    }
  }

  for (let i = 0; i < length; ++i) {
    parseColumns.forEach((column) => {
      const key = column.dataIndex as string
      calculateCount(key, Reflect.get(dataSource[i], key), i)
    })
  }
  return cellToIndexMap
}

使用

让所有的列都进行单元格合并

// dataSource 用自己表格的数据
// columns 为你定义的列
const cellToIndexMap = useAutoMergeTableCell(dataSource, columns)
const parseColumns = columns.map((m) => {
  return {
      ...m,
      onCell: (_, index) => {
        const indexArray = cellToIndexMap.get(m.dataIndex as keyof T)
        return {
          rowSpan: indexArray![index!] ?? 1
        }
      }
  } as TableColumnProps
  return m
})

只想部分列进行合并

只需要传递对应的想要合并的 key 即可

const cellToIndexMap = useAutoMergeTableCell(dataSource, ['key1', 'key2'])
const parseColumns = columns.map((m) => {
  return {
      ...m,
      onCell: (_, index) => {
        const indexArray = cellToIndexMap.get(m.dataIndex as keyof T)
        return {
          rowSpan: indexArray![index!] ?? 1
        }
      }
  } as TableColumnProps
  return m
})

关于element-ui

如果想要适配于 element-ui,只需要把 hooks 中的 dataIndex 替换为 prop 即可。

使用:

const cellToIndexMap = useAutoMergeTableCell(dataSource, ['key1', 'key2'])
// or
// const cellToIndexMap = useAutoMergeTableCell(dataSource, columns)
spanMethods ({ row, column, rowIndex, columnIndex }) {
  const indexArray = cellToIndexMap.get(column.prop)
  return {
    rowSpan: indexArray![index!] ?? 1
    colspan: 1
  }
}

http://www.niftyadmin.cn/n/5664676.html

相关文章

【运维方案】软件运维服务方案(word)

1.项目情况 2.服务简述 2.1服务内容 2.2服务方式 2.3服务要求 2.4服务流程 2.5工作流程 2.6业务关系 2.7培训 3.资源提供 3.1项目组成员 3.2服务保障 进主页学习更多获取更多资料&#xff5e;

字节飞书-测开日常实习-部分手撕代码题

之前的文章提到了一道高频题&#xff1a;最长不重复的字串&#xff0c;用到动态窗口。解法就在之前的文章。这篇文章从牛客上找了一些手撕题&#xff0c;在这里记录分享一下。 1.将给定的字符串中的每个单词的首字母转化为大小字母【简单】 首字母大写__牛客网 不难 就是考察…

虚拟DOM介绍

工作流程 虚拟 DOM 并不直接发生在用户界面构建之前&#xff0c;而是作为构建用户界面过程中的一个重要部分。具体来说&#xff0c;虚拟 DOM 的工作流程如下&#xff1a; 初始化阶段&#xff1a; 组件定义&#xff1a;在应用程序开发过程中&#xff0c;开发者首先定义组件和它…

CMakeLists.txt的学习了解

CMakeLists.txt 是 CMake 构建系统中的配置文件&#xff0c;用于定义项目的编译规则和依赖关系。CMake 是一种跨平台的构建系统&#xff0c;支持从源代码生成编译脚本&#xff08;如 Makefile 或 Visual Studio 工程文件&#xff09;。CMakeLists.txt 通过指定项目信息、源文件…

【AI学习笔记】初学机器学习西瓜书概要记录(二)常用的机器学习方法篇

初学机器学习西瓜书的概要记录&#xff08;一&#xff09;机器学习基础知识篇(已完结) 初学机器学习西瓜书的概要记录&#xff08;二&#xff09;常用的机器学习方法篇(持续更新) 初学机器学习西瓜书的概要记录&#xff08;三&#xff09;进阶知识篇(待更) 文字公式撰写不易&am…

上汽集团社招入职SHL测评:语言理解及数字推理高分攻略、真题题库

上汽集团社招待遇 上汽集团作为国内领先的汽车制造企业&#xff0c;其社招待遇和面试问题一直是求职者关注的焦点。以下是根据最新信息整理的上汽集团社招待遇及面试问题概览&#xff1a; 工资待遇&#xff1a;上汽集团的工资待遇在国内汽车行业中属于较高水平。根据不同职位和…

【MQTT协议使用总结】基于-FreeRTOS平台-移植MQTT协议栈

文章目录 仓库地址关键接口适配FreeRTOS_readFreeRTOS_writeNetworkInit && NetworkConnect && NetworkDisconnect 总结 仓库地址 https://github.com/eclipse/paho.mqtt.embedded-c 这里官方给了一些平台适配案例&#xff0c;这里参考FreeRTOS的 关键接口适配…

1. 如何在Java中连接MySQL数据库?请解释使用JDBC连接的步骤。

要在Java中连接MySQL数据库&#xff0c;通常使用JDBC&#xff08;Java Database Connectivity&#xff09;API。这是一个用于执行SQL语句的Java API&#xff0c;可以用来访问关系型数据库。下面是使用JDBC连接MySQL数据库的详细步骤&#xff1a; 1. 添加MySQL JDBC驱动 首先&a…