2016-03-31 09:40:55 +08:00
---
2019-11-15 14:35:25 +08:00
order: 17
2016-08-15 07:54:01 +08:00
title:
en-US: Tree data
zh-CN: 树形数据展示
2016-03-31 09:40:55 +08:00
---
2015-12-25 15:46:15 +08:00
2016-08-15 07:54:01 +08:00
## zh-CN
2018-11-07 17:37:58 +08:00
表格支持树形数据的展示,当数据中有 `children` 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 `childrenColumnName` 进行配置。
可以通过设置 `indentSize` 以控制每一层的缩进宽度。
2015-12-25 15:46:15 +08:00
2016-08-15 08:07:03 +08:00
## en-US
2018-11-07 17:37:58 +08:00
Display tree structure data in Table when there is field key `children` in dataSource, try to customize `childrenColumnName` property to avoid tree table structure.
You can control the indent width by setting `indentSize` .
2016-08-15 08:07:03 +08:00
2022-05-19 09:46:26 +08:00
```tsx
2022-05-23 14:37:16 +08:00
import { Space, Switch, Table } from 'antd';
2022-07-04 22:09:54 +08:00
import type { ColumnsType } from 'antd/es/table';
import type { TableRowSelection } from 'antd/es/table/interface';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2015-12-25 15:46:15 +08:00
2022-05-19 09:46:26 +08:00
interface DataType {
key: React.ReactNode;
name: string;
age: number;
address: string;
children?: DataType[];
}
const columns: ColumnsType< DataType > = [
2019-05-07 14:57:32 +08:00
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '12%',
},
{
title: 'Address',
dataIndex: 'address',
width: '30%',
key: 'address',
},
];
2015-12-25 15:46:15 +08:00
2022-05-19 09:46:26 +08:00
const data: DataType[] = [
2019-05-07 14:57:32 +08:00
{
key: 1,
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [
{
key: 11,
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
},
{
key: 12,
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [
{
key: 121,
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
},
],
},
{
key: 13,
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [
{
key: 131,
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [
{
key: 1311,
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
},
{
key: 1312,
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
},
],
},
],
},
],
},
{
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
];
2015-12-25 15:46:15 +08:00
2016-10-02 08:35:06 +08:00
// rowSelection objects indicates the need for row selection
2022-05-19 09:46:26 +08:00
const rowSelection: TableRowSelection< DataType > = {
2016-11-15 12:01:06 +08:00
onChange: (selectedRowKeys, selectedRows) => {
2016-03-16 16:39:53 +08:00
console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
},
2016-11-15 12:01:06 +08:00
onSelect: (record, selected, selectedRows) => {
2016-03-16 16:39:53 +08:00
console.log(record, selected, selectedRows);
},
2016-11-15 12:01:06 +08:00
onSelectAll: (selected, selectedRows, changeRows) => {
2016-03-16 16:39:53 +08:00
console.log(selected, selectedRows, changeRows);
2016-05-03 14:15:29 +08:00
},
2016-03-16 16:39:53 +08:00
};
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
const [checkStrictly, setCheckStrictly] = useState(false);
2020-06-28 22:41:59 +08:00
return (
< >
< Space align = "center" style = {{ marginBottom: 16 } } >
CheckStrictly: < Switch checked = {checkStrictly} onChange = {setCheckStrictly} / >
< / Space >
< Table
columns={columns}
rowSelection={{ ...rowSelection, checkStrictly }}
dataSource={data}
/>
< />
);
2022-05-19 09:46:26 +08:00
};
2020-06-28 22:41:59 +08:00
2022-05-19 09:46:26 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```