使用AntD的表格组件中的rowSpan属性来实现单元格合并显示数据。例如,我们可以将下面的数据中“Jack”单元格和“Lucy”单元格合并展示:
import { Table } from 'antd';
const data = [
{
key: '1',
name: 'Jack',
age: 32,
},
{
key: '2',
name: 'Lucy',
age: 26,
},
{
key: '3',
name: 'Tom',
age: 18,
},
{
key: '4',
name: 'John',
age: 24,
},
];
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
render: (text, record, index) => {
const obj = {
children: text,
props: {},
};
if (text === 'Jack') {
obj.props.rowSpan = 2;
}
if (text === 'Lucy') {
obj.props.rowSpan = 0;
}
if (text === 'Tom') {
obj.props.colSpan = 2;
}
return obj;
},
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
render: (text, record, index) => ({
children: text,
props: {},
}),
}
];
在render函数中根据数据内容动态设置rowSpan和colSpan属性即可实现单元格合并。注意:设置rowSpan时,需要将rowSpan的值减1(因为表头占用1行)。
上一篇:Antd表格重复列的问题。
下一篇:antd表格中行之间的间距