You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.1 KiB
139 lines
3.1 KiB
9 years ago
|
---
|
||
5 years ago
|
order: 17
|
||
8 years ago
|
title:
|
||
|
en-US: Tree data
|
||
|
zh-CN: 树形数据展示
|
||
9 years ago
|
---
|
||
9 years ago
|
|
||
8 years ago
|
## zh-CN
|
||
|
|
||
6 years ago
|
表格支持树形数据的展示,当数据中有 `children` 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 `childrenColumnName` 进行配置。
|
||
|
|
||
|
可以通过设置 `indentSize` 以控制每一层的缩进宽度。
|
||
9 years ago
|
|
||
8 years ago
|
## en-US
|
||
|
|
||
6 years ago
|
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`.
|
||
8 years ago
|
|
||
6 years ago
|
```jsx
|
||
4 years ago
|
import { Table, Switch, Space } from 'antd';
|
||
9 years ago
|
|
||
6 years ago
|
const columns = [
|
||
|
{
|
||
|
title: 'Name',
|
||
|
dataIndex: 'name',
|
||
|
key: 'name',
|
||
|
},
|
||
|
{
|
||
|
title: 'Age',
|
||
|
dataIndex: 'age',
|
||
|
key: 'age',
|
||
|
width: '12%',
|
||
|
},
|
||
|
{
|
||
|
title: 'Address',
|
||
|
dataIndex: 'address',
|
||
|
width: '30%',
|
||
|
key: 'address',
|
||
|
},
|
||
|
];
|
||
9 years ago
|
|
||
6 years ago
|
const data = [
|
||
|
{
|
||
|
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',
|
||
|
},
|
||
|
];
|
||
9 years ago
|
|
||
8 years ago
|
// rowSelection objects indicates the need for row selection
|
||
9 years ago
|
const rowSelection = {
|
||
8 years ago
|
onChange: (selectedRowKeys, selectedRows) => {
|
||
9 years ago
|
console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
|
||
|
},
|
||
8 years ago
|
onSelect: (record, selected, selectedRows) => {
|
||
9 years ago
|
console.log(record, selected, selectedRows);
|
||
|
},
|
||
8 years ago
|
onSelectAll: (selected, selectedRows, changeRows) => {
|
||
9 years ago
|
console.log(selected, selectedRows, changeRows);
|
||
9 years ago
|
},
|
||
9 years ago
|
};
|
||
|
|
||
4 years ago
|
function TreeData() {
|
||
|
const [checkStrictly, setCheckStrictly] = React.useState(false);
|
||
|
return (
|
||
|
<>
|
||
|
<Space align="center" style={{ marginBottom: 16 }}>
|
||
|
CheckStrictly: <Switch checked={checkStrictly} onChange={setCheckStrictly} />
|
||
|
</Space>
|
||
|
<Table
|
||
|
columns={columns}
|
||
|
rowSelection={{ ...rowSelection, checkStrictly }}
|
||
|
dataSource={data}
|
||
|
/>
|
||
|
</>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
ReactDOM.render(<TreeData />, mountNode);
|
||
6 years ago
|
```
|