Ant Design Pro Table 合并单元格 - 详细教程
Ant Design Pro Table 提供了 rowSpan 和 colSpan 两个属性来合并单元格。具体操作如下:
- 在
columns中设置需要合并的列的rowSpan和colSpan属性:
const columns = [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
rowSpan: 2, // 合并两行
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
colSpan: 2, // 合并两列
},
{
title: '性别',
dataIndex: 'gender',
key: 'gender',
colSpan: 0, // 不需要渲染该列
},
{
title: '电话',
dataIndex: 'phone',
key: 'phone',
rowSpan: 2, // 合并两行
},
{
title: '城市',
dataIndex: 'city',
key: 'city',
rowSpan: 2, // 合并两行
},
];
- 在
dataSource中设置需要合并的单元格的值为undefined:
const dataSource = [
{
key: '1',
name: '张三',
age: 18,
gender: undefined, // 注意设置为 undefined
phone: '18888888888',
city: '北京',
},
{
key: '2',
name: '李四',
age: undefined, // 注意设置为 undefined
gender: '男',
phone: '17777777777',
city: '上海',
},
];
- 在
render中判断每个单元格是否需要渲染:
<ProTable
columns={columns}
dataSource={dataSource}
rowKey="key"
pagination={false}
bordered
size="small"
scroll={{ y: 300 }}
renderCell={(text, record, rowIndex, colIndex) => {
const { rowSpan, colSpan } = columns[colIndex];
if (rowSpan === 0 || colSpan === 0) {
return null;
}
if (rowSpan > 1 && rowIndex % rowSpan !== 0) {
return null;
}
if (colSpan > 1 && colIndex % colSpan !== 0) {
return null;
}
return <span>{text}</span>;
}}
/>
在 renderCell 中,首先判断该单元格是否需要渲染,如果 rowSpan 或 colSpan 为 0,则不需要渲染;如果 rowSpan 或 colSpan 大于 1,则需要判断当前行或列是否需要渲染。最后返回渲染的内容。
以上就是 Ant Design Pro Table 如何合并单元格的操作步骤。
原文地址: https://www.cveoy.top/t/topic/nuR6 著作权归作者所有。请勿转载和采集!