ant-design/components/tree/demo/dynamic.md

69 lines
1.5 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 3
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.
2017-02-13 10:55:53 +08:00
````jsx
import { Tree } from 'antd';
const TreeNode = Tree.TreeNode;
2015-10-27 15:47:01 +08:00
class Demo extends React.Component {
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 },
],
}
onLoadData = (treeNode) => {
2016-01-07 19:05:55 +08:00
return new Promise((resolve) => {
2017-09-13 22:06:53 +08:00
if (treeNode.props.children) {
resolve();
return;
}
2016-01-07 19:05:55 +08:00
setTimeout(() => {
2017-09-13 22:06:53 +08:00
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],
});
2016-01-07 19:05:55 +08:00
resolve();
2016-07-13 16:12:44 +08:00
}, 1000);
2015-10-27 15:47:01 +08:00
});
}
2017-09-13 22:06:53 +08:00
renderTreeNodes = (data) => {
return data.map((item) => {
2016-01-28 15:13:17 +08:00
if (item.children) {
2017-09-13 22:06:53 +08:00
return (
<TreeNode title={item.title} key={item.key} dataRef={item}>
{this.renderTreeNodes(item.children)}
</TreeNode>
);
2016-01-28 15:13:17 +08:00
}
2017-09-13 22:06:53 +08:00
return <TreeNode {...item} dataRef={item} />;
2016-01-28 15:13:17 +08:00
});
2017-09-13 22:06:53 +08:00
}
render() {
2015-10-27 15:47:01 +08:00
return (
2017-09-13 22:06:53 +08:00
<Tree loadData={this.onLoadData}>
{this.renderTreeNodes(this.state.treeData)}
2016-01-28 15:13:17 +08:00
</Tree>
);
}
}
2015-10-27 15:47:01 +08:00
2016-01-07 19:05:55 +08:00
ReactDOM.render(<Demo />, mountNode);
2015-10-27 15:47:01 +08:00
````