当前位置: 首页 > 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}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 路由原理介绍
  • Springboot错误日志切面,找到post请求体被消费后的数据
  • Unity-Transform类-缩放和看向
  • CTFHub技能树-信息泄露-HG泄漏
  • linux-硬件与设备管理-硬件信息查看
  • 信息安全工程师(6)网络信息安全现状与问题
  • TI AM62X Secure Boot 流程简述
  • Python计算机视觉第十章-OpenCV
  • 开源项目 face parsing 人脸区域分割 人像区域分割 人脸分割 人像区域分割 BiSeNet
  • [mysql]mysql排序和分页
  • 9.18 微信小程序开发笔记
  • vue-ts-demo
  • 二十种编程语言庆祝中秋节
  • 若依笔记(六):前后端token鉴权体系
  • 初始爬虫1(补充)
  • 【mysql】环境安装、服务启动、密码设置
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Angularjs之国际化
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • JavaScript-Array类型
  • log4j2输出到kafka
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Tornado学习笔记(1)
  • Vim Clutch | 面向脚踏板编程……
  • 从tcpdump抓包看TCP/IP协议
  • 从重复到重用
  • 欢迎参加第二届中国游戏开发者大会
  • 基于 Babel 的 npm 包最小化设置
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 正则与JS中的正则
  • Android开发者必备:推荐一款助力开发的开源APP
  • Semaphore
  • 容器镜像
  • 整理一些计算机基础知识!
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​VRRP 虚拟路由冗余协议(华为)
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (175)FPGA门控时钟技术
  • (19)夹钳(用于送货)
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (Git) gitignore基础使用
  • (NSDate) 时间 (time )比较
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (论文阅读30/100)Convolutional Pose Machines
  • (一) springboot详细介绍
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)ORM