前情


公司有经常需要做一些后台管理页面,我们选择了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. chatGPT能做职业规划?看完之后发现3年软测白做了!

    "每天都是重复.单调的工作,收入不理想,想跳槽无力,学习又没有动力和方向,不知道未来的发展在哪里,甚至想转行·····" 做测试久了,很多人都有诸如此类的疑惑,不想一直停留在测试需 ...

  2. 4Templates Bootstrap Navbars and Links

    链接 传递参数    

  3. 解决每次调试flask需要重启本地服务器的方法

    export FLASK_ENV=development export FLASK_APP=hello.py flask run --debug

  4. 北京智和信通PON.EPON.GPON运维解决方案,全面管理OLT.ONU等设备

    高质量.高可靠.高安全性的网络已成为助力企事业单位高速发展的基石.PON网络采用先进的无源光纤通信技术与自动化融合,构建新兴一体化网络体系,可以有效构造安全可靠的生产办公网络.因此,交通.制造.能源. ...

  5. 八字测算引流seo程序php网页版黄历/排盘/抽签/星座/生肖/解梦整站程序分享

    2演示站: https://s31.yczfcn.com/ 2源码说明: 1.手机端和PC端共两套模板,手机端访问时候自动跳转至手机端模板和域名. 2.本程序包含文章系统,结合自身的免费测算功能,适合 ...

  6. Sealos Devbox 发布,珍爱生命,远离 CI/CD

    水滴攻击太阳系用的是最原始的攻击方式:撞击!却又如此有效率. 当我们搞了一堆容器.编排.CI/CD.DevOps,发明了一大堆没什么用的名词之后,最终发现这些操作都是花里胡哨,让开发者越陷越深. 最终 ...

  7. 如何使用hardware breakpoint

    要使用内核的硬件断点(hardware breakpoint)来定位内核模块中的内存访问问题,你可以通过以下步骤进行设置和调试. 1. 确定要监控的内存地址 首先,你需要确定你想要监控的内存地址.这可 ...

  8. 创建一个简单的基于MyBatis的项目

  9. is特性

    is是特性在动态路由的时候使用 ,在挂载点 component 使用,用来判断哪个组件显示 :

  10. excel导⼊功能的实现流程简要描述⼀下?

    当时公司的场景⼤概⼀个excel⽂件⾥就⼏⼗条数据,量⽐较少,和后端商量之后制定了前端主导的⽅ 案,解析的过程放到了浏览器端做,当时是参考了⼀下vue-admin中的现成的⽅案 ⼤概流程是这样的,⼈事 ...