前情


公司有经常需要做一些后台管理页面,我们选择了Ant Design Pro,它是基于 Ant Design 和 umi 的封装的一整套企业级中后台前端/设计解决方案。

产品效果图


最新接到的一个后台管理界面需求,需要根据相同的itemId做单元格合并,要实现的效果如下:

解决方案

方式1:使用表格的rowspan属性来实现单元格合并

关键示例代码:

/**
* 实现单元格合并
* 在原始数据中加上rowSpan属性,该值为接下来和当前项itemId相同的个数+1,
* 其它相同的项rowSpan设为0即为不显示
* @param list
*/
const dataProcessingForRowspan = (list, key) => {
let repeatCount = 1;
let startIndex = 0;
list.forEach((item, index) => {
if (index < list.length - 1 && item.itemId === list[index+1].itemId) {
item.rowSpan = 0;
repeatCount += 1
} else {
list[startIndex].rowSpan = repeatCount;
startIndex = index + 1;
repeatCount = 1;
}
})
} const columns: ProColumns<API.EduAuthenticatedUserItem>[] = [
{
title: '商品itemId',
dataIndex: 'itemId',
editable: false,
render: (text: any, record: any, index: number) => {
return {
children: text || '',
props: {
rowSpan: record.rowSpan ? record.rowSpan : 0
}
}
}
},
...
{
title: '操作',
dataIndex: 'option',
valueType: 'option',
render: (text, record:any, index) => {
return {
children: [
<a
key="config"
onClick={() => {
}}
>
拉取sku
</a>,
' | '
,
<a
key="delete"
onClick={() => {
}}
>
删除
</a>,
],
props: {
rowSpan: record.rowSpan ? record.rowSpan : 0
}
}
},
},
]; return (
<PageContainer>
<ProTable<API.EduAuthenticatedUserItem, API.AuthenticatedUserParams>
headerTitle=''
rowKey="skuId"
search={{
labelWidth: 120,
}}
toolBarRender={() => [
]}
request={async (params) => {
await waitTime(3000)
// 这里是模拟的数据
const initData = [{
itemId: '1',
name: '魅族20',
skuId: 'a123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 3,
estimatedDeduction: 49.18
},{
itemId: '1',
name: '魅族20',
skuId: 'b123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 2,
estimatedDeduction: 49.18
},{
itemId: '2',
name: '魅族20',
skuId: 'c123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
},{
itemId: '2',
name: '魅族20',
skuId: 'd123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
},{
itemId: '2',
name: '魅族20',
skuId: 'e123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
},{
itemId: '3',
name: '魅族20',
skuId: 'f123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
}] dataProcessingForRowspan(initData, 'itemId');
return {
data: initData,
// success 请返回 true,
// 不然 table 会停止解析数据,即使有数据
success: true,
// 不传会使用 data 的长度,如果是分页一定要传
total: 5,
};
}}
columns={columns}
/>
</PageContainer>
);

方式2:直接自定义reander方法,在一个单元格中生成遍历的内容,内容块之间加下边框,实现伪单元格合并效果

关键示例代码:

const columns: ProColumns<API.EduAuthenticatedUserItem>[] = [
{
title: '商品itemId',
dataIndex: 'itemId',
editable: false,
},
{
title: '商品名',
dataIndex: 'itemName',
},
{
title: 'skuId',
dataIndex: 'itemSkuList',
key: 'skuId',
render: (text: any, record: any) => {
return record.itemSkuList.map((item: any, indexIn: number) => {
return (
<div
key={item.skuId + '' + indexIn}
style={{
borderBottom: indexIn === record.itemSkuList.length - 1 ? '' : '1px solid #f0f0f0',
}}
>
<div
style={{
width: '100%',
height: '50px',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
margin: 'auto',
}}
>
{item['skuId']}
</div>
</div>
);
});
},
},
...
{
title: '预估抵扣',
dataIndex: 'itemSkuList',
key: 'predictDeductionPrice',
editable: false,
hideInSearch: true,
render: (text: any, record: any) => {
return record.itemSkuList.map((item: any, indexIn: number) => {
return (
<div
key={item.skuId + '' + indexIn}
style={{
borderBottom: indexIn === record.itemSkuList.length - 1 ? '' : '1px solid #f0f0f0',
}}
>
<div
style={{
width: '100%',
height: '50px',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
margin: 'auto',
}}
>
{item['predictDeductionPrice']}
</div>
</div>
);
});
},
},
{
title: '操作',
dataIndex: 'option',
valueType: 'option',
render: (text: any, record: any) => {
return (
<>
<a
key="config"
onClick={async () => {
}}
>
拉取sku
</a>{' '}
|{' '}
<a
key="delete"
onClick={async () => {
}}
>
删除
</a>
</>
);
},
},
]; return (
<PageContainer>
<ProTable<API.EduAuthenticatedUserItem, API.AuthenticatedUserParams>
headerTitle=''
rowKey="itemId"
search={{
labelWidth: 120,
}}
toolBarRender={() => [
]}
request={async (params) => {
await waitTime(3000)
const initData = [{
itemId: '1',
itemSkuList:[
{
name: '魅族20',
skuId: 'a123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 3,
estimatedDeduction: 49.18
},{
name: '魅族20',
skuId: 'b123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 2,
estimatedDeduction: 49.18
},{
name: '魅族20',
skuId: 'c123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
},{ }
]
},{
itemId: '2',
itemSkuList: [
{
name: '魅族20',
skuId: 'd123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
},{
name: '魅族20',
skuId: 'e123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
}
] },{
itemId: '3',
name: '魅族20',
skuId: 'f123456b',
productAbbreviation: '魅族20 独白 8GB+256GB 全网通公开版',
price: 2459,
maximumDeduction: 1,
estimatedDeduction: 49.18
}] dataProcessingForRowspan(initData, 'itemId');
return {
data: initData,
// success 请返回 true,
// 不然 table 会停止解析数据,即使有数据
success: true,
// 不传会使用 data 的长度,如果是分页一定要传
total: 5,
};
}}
columns={columns}
/>
</PageContainer>
);
  • 注:二种方式是对不同的数据结构做的更优的方法,如果只想用其中一种,二种数据结构互转一下也可以的。

Ant Design Pro项目ProTable怎么实现单元格合并效果的更多相关文章

  1. Ant Design Pro项目打开页设为登录或者其他页面

    Ant Design Pro项目打开页设为登录或者其他页面 一.打开页设为登录页 首先找到utils包中的authority文件,在该文件中找到如下代码: export function getAut ...

  2. 修改TreeList单元格格式(实现类似单元格合并效果)

    关键点:(1)TreeList中显示的单元格默认不显示上.下.左.右边框,显示的是TreeList自身的行横边框.列纵边框,具体对应TreeList属性中OptionView项下的ShowVertLi ...

  3. 【后台管理系统】—— Ant Design Pro 项目爬坑(一)

    1.表单rule中可以分对象处理不同规则 <FormItem label="类型名称" {...formLayout}> { form.getFieldDecorato ...

  4. element-ui 格式化树形数组在table组件中展示(单元格合并)

    最近做的项目涉及到很多单元格合并问题,element-ui组件对于单元格合并的处理虽然很灵活,但是需要事先计算好每个单元格合并的rowspan和colspan,直接在span-method属性中计算实 ...

  5. ant design pro(一)安装、目录结构、项目加载启动【原始、以及idea开发】

    一.概述 1.1.脚手架概念 编程领域中的“脚手架(Scaffolding)”指的是能够快速搭建项目“骨架”的一类工具.例如大多数的React项目都有src,public,webpack配置文件等等, ...

  6. 使用ant design pro搭建项目

    脚手架搭建 git clone --depth=1 https://github.com/ant-design/ant-design-pro.git my-project 然后 cd my-proje ...

  7. 阿里开源项目之Ant Design Pro

    本篇文章主要包含的内容有三个方面. 第一.Ant Design Pro简介; 第二.Ant Design Pro能做什么; 第三.初步使用; 我相信通过这三个方面的讲解能让你大概知道Ant Desig ...

  8. 【后台管理系统】—— Ant Design Pro入门学习&项目实践笔记(三)

    前言:前一篇记录了[后台管理系统]目前进展开发中遇到的一些应用点,这一篇会梳理一些自己学习Ant Design Pro源码的功能点.附:Ant Design Pro 在线预览地址. Dashboard ...

  9. Ant Design Pro+Electron+electron-builder实现React应用脱离浏览器,桌面安装运行

    ant-design-pro ----> version :2.3.1 由于网上Ant Design Pro+Electron的资料太少,我就贡献一点经验   最近需要讲AntD Pro项目(以 ...

  10. ant design pro (十五)advanced 使用 API 文档工具

    一.概述 原文地址:https://pro.ant.design/docs/api-doc-cn 在日常开发中,往往是前后端分离的,这个时候约定好一套接口标准,前后端各自独立开发,就不会被对方的技术难 ...

随机推荐

  1. SSD-KD:天翼云&清华出品,最新无原始数据的蒸馏研究 | CVPR'24

    无数据知识蒸馏能够利用大型教师网络所学到的知识,来增强较小型学生网络的训练,而无需访问原始训练数据,从而避免在实际应用中的隐私.安全和专有风险.在这方面的研究中,现有的方法通常遵循一种反演蒸馏的范式, ...

  2. WebRTC 初探

    背景 我正在实现一个 FC 游戏网站, PC 用户仅需要配置键盘便能实现小伙伴们一起玩, 但是手机用户就比较麻烦了 传统的网页游戏都是通过 HTTP/WS 的方式实现联机, 对于服务器的负担还是比较重 ...

  3. CTFSHOW pwn03 WrriteUp

    本文来自一个初学CTF的小白,如有任何问题请大佬们指教! 题目来源 CTFShow pwn - pwn03 (ret2libc) https://ctf.show/challenges 思路 1.下载 ...

  4. form data 与request payload的区别以及php接收这些数据的方法

    form data 与request payload的区别以及php接收这些数据的方法 以前与前端交互一直都是POST.GET的,PHP端就直接$_POST,$_GET来接收,从来没有出现过以外. 最 ...

  5. ftrace irqs跟踪器

    当中断被关闭(俗称关中断)了,CPU就不能响应其他的事件,如果这时有一个鼠标中断,要在下一次开中断时才能响应这个鼠标中断,这段延迟称为中断延迟.向current_tracer 文件写入 irqsoff ...

  6. python安装pandas+pytz

    如下图所示,在安装pandas的过程中,发现他还要安装pytz这个包.我不想等他自己下载,因为很容易出错,所以我就先下载了pytz这个包,然后安装完毕,再去安装pandas这个包. 首先呢先登录这个网 ...

  7. 基于Python后端构建多种不同的系统终端界面研究

    在我们一般开发系统的时候,往往会根据实际需要做出各种不同的系统终端界面,如基于BS的,CS.APP.小程序等等,一般都是基于一个统一接入的Web API后端,本篇系统探寻对基于Python后端构建多种 ...

  8. yarn 命令大全

    npm install yarn -g npm install --global yarnyarn 中文网:https://yarn.bootcss.com/docs/install/#windows ...

  9. AOT漫谈专题(第四篇): C#程序如何编译成Native代码

    一:背景 1. 讲故事 大家都知道所谓的.NET Native AOT即通过AOT编译器直接将C#代码编译成机器码,大家也习惯用C/C++的编译过程来类比,都是静态编译本质上都差不多,这篇我们借助工具 ...

  10. 三大主流负载均衡软件对比(LVS+Nginx+HAproxy)

    LVS: 优点 : 1.抗负载能力强.性能高,能达到F5硬件的60%:对内存和cpu资源消耗比较低2.工作在网络4层,通过vrrp协议转发(仅作分发之用),具体的流量由linux内核处理,因此没有流量 ...