我已将列更改为有孩子。它非常接近您想要实现的目标。
const columns = [
{
title: "District",
dataIndex: "state_name",
key: "state_name",
render: (value, row, index) => {
const obj = {
children: value,
props: {}
};
console.log(obj.children, index);
if (index % 3 === 0) {
obj.props.rowSpan = 3;
}
// These two are merged into above cell
if (index % 3 === 1) {
obj.props.rowSpan = 0;
}
if (index % 3 === 2) {
obj.props.rowSpan = 0;
}
return obj;
}
},
{
title: "Exam Details",
children: [
{
title: "Gender Details",
dataIndex: "gender",
key: 1
},
{
title: "Total number of candidates",
dataIndex: "total",
key: 2
},
{
title: "Total Number of candidates passed",
dataIndex: "passed_total",
key: 3
}
]
}
];
const data = [
{
state_name: "Karnataka",
gender: "Boys",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Karnataka",
gender: "Girls",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Karnataka",
gender: "Transgender",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Kerala",
gender: "Boys",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Kerala",
gender: "Girls",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Kerala",
gender: "Transgender",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Tamilnadu",
gender: "Boys",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Tamilnadu",
gender: "Girls",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Tamilnadu",
gender: "Transgender",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Goa",
gender: "Boys",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Goa",
gender: "Girls",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Goa",
gender: "Transgender",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Andhra Pradesh",
gender: "Boys",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Andhra Pradesh",
gender: "Girls",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
},
{
state_name: "Andhra Pradesh",
gender: "Transgender",
total: Math.floor(Math.random() * 90 + 10),
passed_total: Math.floor(Math.random() * 90 + 10)
}
];
Sandbox url
@更新
解决了这个问题,基本上外面有一个变量,当sameKey重复不设置计数时rowSpan为0,所以它会被隐藏。
逻辑
let sameKey;
const columns = [
{
title: "District",
dataIndex: "state_name",
key: "state_name",
render: (value, row, index) => {
const obj = {
children: value,
props: {}
};
if (!(sameKey !== value)) {
obj.props.rowSpan = 0;
return obj;
}
const count = data.filter(item => item.state_name === value).length;
sameKey = value;
obj.props.rowSpan = count;
return obj;
}
},
使用动态数量的性别行工作 codesandbox