当前位置: 首页 > news >正文

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

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

合并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.lengthconst 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 stringcalculateCount(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 TableColumnPropsreturn 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 TableColumnPropsreturn 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!] ?? 1colspan: 1}
}
http://www.lryc.cn/news/440924.html

相关文章:

  • 通过多模态关系图学习实现可解释的医学图像视觉问答|文献速递--Transformer架构在医学影像分析中的应用
  • 从入门到精通,带你探索适合新手的视频剪辑工具
  • 线性规划------ + 案例 + Python源码求解(见文中)
  • 用Java实现人工智能
  • MobaXterm使用技巧
  • openstack中的rabbitmq
  • etcd三节点,其中一个坏掉了的恢复办法
  • 计算机毕业设计 基于SpringBoot框架的网上蛋糕销售系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • C编程控制PC蜂鸣器方法2
  • C# SQL 辅助工具
  • eNSP简单用法
  • 1035. 不相交的线
  • 1.pytest基础知识(默认的测试用例的规则以及基础应用)
  • Linux常见查看文件命令
  • 初识 performance_schema:轻松掌握MySQL性能监控
  • linux下top命令查看和解释
  • 换个手机IP地址是不是不一样?
  • 【从计算机的发展角度理解编程语言】C、CPP、Java、Python,是偶然还是应时代的产物?
  • 《Google软件测试之道》笔记
  • 实战讲稿:Spring Boot整合MyBatis
  • 基于深度学习的眼部疾病检测识别系统
  • curl格式化json之jq工具?
  • 百收SEO蜘蛛池
  • (娱乐)魔改浏览器-任务栏图标右上角加提示徽章
  • JVM相关
  • 9.18 微信小程序开发笔记
  • dpdk课程学习之练习笔记八(dpvs的了解)
  • Linux标准IO-系统调用详解
  • LeetCode004-两个有序数组的中位数-最优算法代码讲解
  • Unity携程Coroutine用法