帮我用antdesign+react实现三个表格嵌套效果
时间: 2024-02-21 21:58:56 浏览: 91
好的,下面是一个简单的实现示例:
```jsx
import React from "react";
import { Table } from "antd";
const columns1 = [
{
title: "姓名",
dataIndex: "name",
key: "name"
},
{
title: "年龄",
dataIndex: "age",
key: "age"
},
{
title: "住址",
dataIndex: "address",
key: "address"
}
];
const columns2 = [
{
title: "学科",
dataIndex: "subject",
key: "subject"
},
{
title: "分数",
dataIndex: "score",
key: "score"
}
];
const columns3 = [
{
title: "项目",
dataIndex: "project",
key: "project"
},
{
title: "进度",
dataIndex: "progress",
key: "progress"
}
];
const data1 = [
{
key: "1",
name: "张三",
age: 18,
address: "北京市朝阳区"
},
{
key: "2",
name: "李四",
age: 20,
address: "上海市浦东新区"
}
];
const data2 = [
{
key: "1",
subject: "数学",
score: 90
},
{
key: "2",
subject: "语文",
score: 95
}
];
const data3 = [
{
key: "1",
project: "设计",
progress: "80%"
},
{
key: "2",
project: "开发",
progress: "60%"
}
];
class NestedTable extends React.Component {
constructor(props) {
super(props);
this.state = {
expandedRowKeys: []
};
}
onExpand = (expanded, record) => {
const { expandedRowKeys } = this.state;
if (expanded) {
this.setState({
expandedRowKeys: [...expandedRowKeys, record.key]
});
} else {
this.setState({
expandedRowKeys: expandedRowKeys.filter(key => key !== record.key)
});
}
};
render() {
const { expandedRowKeys } = this.state;
const expandedRowRender1 = record => (
<Table
columns={columns2}
dataSource={data2}
pagination={false}
size="small"
showHeader={false}
/>
);
const expandedRowRender2 = record => (
<Table
columns={columns3}
dataSource={data3}
pagination={false}
size="small"
showHeader={false}
/>
);
return (
<Table
columns={columns1}
dataSource={data1}
pagination={false}
size="small"
expandedRowRender={expandedRowRender1}
expandedRowKeys={expandedRowKeys}
onExpand={this.onExpand}
rowKey="key"
>
<Table
columns={columns2}
dataSource={data2}
pagination={false}
size="small"
showHeader={false}
/>
<Table
columns={columns3}
dataSource={data3}
pagination={false}
size="small"
showHeader={false}
/>
</Table>
);
}
}
export default NestedTable;
```
这个示例中,我们使用了三个表格分别表示三个不同的数据源,然后将它们嵌套在了一个表格中,通过 `expandedRowRender` 属性指定了第一个表格展开时应该渲染的内容,同时也提供了可以展开和收起的功能。
阅读全文