2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 3
|
2017-02-19 21:23:38 +08:00
|
|
|
title:
|
2016-07-21 09:52:39 +08:00
|
|
|
zh-CN: 异步数据加载
|
|
|
|
en-US: load data asynchronously
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-10-27 15:47:01 +08:00
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-01-28 15:13:17 +08:00
|
|
|
点击展开节点,动态加载数据。
|
2015-10-27 15:47:01 +08:00
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
To load data asynchronously when click to expand a treeNode.
|
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
```tsx
|
2015-10-29 13:42:40 +08:00
|
|
|
import { Tree } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
const { TreeNode } = Tree;
|
2015-10-27 15:47:01 +08:00
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
const initTreeDate = [
|
|
|
|
{ title: 'Expand to load', key: '0' },
|
|
|
|
{ title: 'Expand to load', key: '1' },
|
|
|
|
{ title: 'Tree Node', key: '2', isLeaf: true },
|
|
|
|
];
|
|
|
|
|
|
|
|
const Demo: React.FC<{}> = () => {
|
|
|
|
const [treeData, setTreeData] = React.useState(initTreeDate);
|
|
|
|
|
|
|
|
function onLoadData({ props: { data } }) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (data.children) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
data.children = [
|
|
|
|
{ title: 'Child Node', key: `${data.key}-0` },
|
|
|
|
{ title: 'Child Node', key: `${data.key}-1` },
|
|
|
|
];
|
|
|
|
setTreeData([...treeData]);
|
|
|
|
resolve();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Tree loadData={onLoadData} treeData={treeData} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Demo1 extends React.Component {
|
2017-02-19 21:23:38 +08:00
|
|
|
state = {
|
2017-09-13 22:06:53 +08:00
|
|
|
treeData: [
|
|
|
|
{ title: 'Expand to load', key: '0' },
|
|
|
|
{ title: 'Expand to load', key: '1' },
|
|
|
|
{ title: 'Tree Node', key: '2', isLeaf: true },
|
|
|
|
],
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
onLoadData = treeNode => {
|
|
|
|
const { treeData } = this.state;
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const { props } = treeNode;
|
2019-05-07 14:57:32 +08:00
|
|
|
if (treeNode.props.children) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
treeNode.props.dataRef.children = [
|
|
|
|
{ title: 'Child Node', key: `${treeNode.props.eventKey}-0` },
|
|
|
|
{ title: 'Child Node', key: `${treeNode.props.eventKey}-1` },
|
|
|
|
];
|
|
|
|
this.setState({
|
|
|
|
treeData: [...this.state.treeData],
|
|
|
|
});
|
|
|
|
resolve();
|
|
|
|
}, 1000);
|
|
|
|
});
|
2019-08-12 13:22:36 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-09-13 22:06:53 +08:00
|
|
|
render() {
|
2019-08-12 13:22:36 +08:00
|
|
|
return <Tree loadData={this.onLoadData} treeData={this.state.treeData} />;
|
2017-02-19 21:23:38 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-27 15:47:01 +08:00
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|