帮我用react+antdesign实现一个表格嵌套两层表格的效果
时间: 2024-02-23 09:02:11 浏览: 92
react+ant design实现Table的增、删、改的示例代码
好的,下面是一个简单的实现示例:
```javascript
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 data1 = [
{ key: "1", name: "John Brown", age: 32, address: "New York No. 1 Lake Park" },
{ key: "2", name: "Jim Green", age: 42, address: "London No. 1 Lake Park" },
{ key: "3", name: "Joe Black", age: 32, address: "Sidney No. 1 Lake Park" },
];
const columns2 = [
{ title: "科目", dataIndex: "subject", key: "subject" },
{ title: "成绩", dataIndex: "score", key: "score" },
];
const data2 = [
{ key: "1", subject: "数学", score: 90 },
{ key: "2", subject: "英语", score: 80 },
{ key: "3", subject: "语文", score: 95 },
];
const expandedRowRender = (record) => {
const subTableData = [
{ key: "1", subject: "数学", score: 90 },
{ key: "2", subject: "英语", score: 80 },
{ key: "3", subject: "语文", score: 95 },
];
const subTableColumns = [
{ title: "科目", dataIndex: "subject", key: "subject" },
{ title: "成绩", dataIndex: "score", key: "score" },
];
return <Table columns={subTableColumns} dataSource={subTableData} pagination={false} />;
};
const TableWithNestedTable = () => {
return (
<Table
columns={columns1}
dataSource={data1}
pagination={false}
expandedRowRender={expandedRowRender}
/>
);
};
export default TableWithNestedTable;
```
首先,我们先定义两个表格的列和数据,分别为 `columns1` 和 `data1`,以及嵌套的表格的列和数据,分别为 `columns2` 和 `data2`。
然后,我们定义一个 `expandedRowRender` 函数,该函数接受一个参数 `record`,表示当前行的数据。在该函数中,我们返回一个嵌套的表格,该表格的列和数据为 `columns2` 和 `data2`。
最后,我们在主表格中添加 `expandedRowRender` 属性,并将其值设置为 `expandedRowRender` 函数,这样就可以实现表格嵌套两层表格的效果了。
注意,我们在嵌套的表格中将 `pagination` 属性设置为 `false`,以禁用分页功能。
阅读全文