2015-10-15 10:38:14 +08:00
|
|
|
# 接收外部传参
|
|
|
|
|
|
|
|
- order: 2
|
|
|
|
|
|
|
|
受控组件,外部传入参数,控制树对象节点
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
````jsx
|
2015-10-29 13:42:40 +08:00
|
|
|
import { Tree, Button } from 'antd';
|
|
|
|
const TreeNode = Tree.TreeNode;
|
2015-10-15 10:38:14 +08:00
|
|
|
|
|
|
|
class TreeDemo extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
['handleClick', 'handleCheck', 'handleSelect'].forEach((m)=> {
|
|
|
|
this[m] = this[m].bind(this);
|
|
|
|
});
|
|
|
|
this.state = {
|
|
|
|
checkedKeys: [],
|
|
|
|
selectedKeys: []
|
2015-11-25 17:35:49 +08:00
|
|
|
};
|
2015-10-15 10:38:14 +08:00
|
|
|
}
|
|
|
|
handleClick() {
|
|
|
|
this.setState({
|
|
|
|
checkedKeys: ['p11'],
|
|
|
|
selectedKeys: ['p21', 'p11']
|
2015-11-25 17:35:49 +08:00
|
|
|
});
|
2015-10-15 10:38:14 +08:00
|
|
|
}
|
|
|
|
handleCheck(info) {
|
|
|
|
console.log('check: ', info);
|
|
|
|
this.setState({
|
|
|
|
checkedKeys: ['p21'],
|
|
|
|
selectedKeys: ['p1', 'p21']
|
2015-11-25 17:35:49 +08:00
|
|
|
});
|
2015-10-15 10:38:14 +08:00
|
|
|
}
|
|
|
|
handleSelect(info) {
|
|
|
|
console.log('selected: ', info);
|
|
|
|
this.setState({
|
|
|
|
checkedKeys: ['p21'],
|
|
|
|
selectedKeys: ['p21']
|
2015-11-25 17:35:49 +08:00
|
|
|
});
|
2015-10-15 10:38:14 +08:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (<div>
|
2015-11-25 17:47:55 +08:00
|
|
|
<Tree defaultExpandAll checkable
|
2015-11-25 17:35:49 +08:00
|
|
|
onCheck={this.handleCheck} checkedKeys={this.state.checkedKeys}
|
|
|
|
onSelect={this.handleSelect} selectedKeys={this.state.selectedKeys} multiple>
|
|
|
|
<TreeNode title="parent 1" key="p1">
|
|
|
|
<TreeNode key="p10" title="leaf"/>
|
|
|
|
<TreeNode title="parent 1-1" key="p11">
|
|
|
|
<TreeNode title="parent 2-1" key="p21">
|
|
|
|
<TreeNode>test</TreeNode>
|
|
|
|
<TreeNode title={<span>sss</span>}/>
|
2015-10-15 10:38:14 +08:00
|
|
|
</TreeNode>
|
2015-11-25 17:35:49 +08:00
|
|
|
<TreeNode key="p22" title="leaf"/>
|
2015-10-15 10:38:14 +08:00
|
|
|
</TreeNode>
|
2015-11-25 17:35:49 +08:00
|
|
|
</TreeNode>
|
|
|
|
<TreeNode key="p12" title="leaf"/>
|
|
|
|
</Tree>
|
2015-10-26 11:02:48 +08:00
|
|
|
<Button type="primary" size="small" onClick={this.handleClick}>点此控制树节点</Button>
|
2015-10-15 10:38:14 +08:00
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 16:47:55 +08:00
|
|
|
ReactDOM.render(<TreeDemo />, document.getElementById('components-tree-demo-special'));
|
2015-10-15 10:38:14 +08:00
|
|
|
|
|
|
|
````
|