2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 1
|
2016-09-01 18:12:12 +08:00
|
|
|
title:
|
2016-07-21 09:52:39 +08:00
|
|
|
zh-CN: 受控操作示例
|
|
|
|
en-US: basic controlled example
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-01-07 19:05:55 +08:00
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
受控操作示例
|
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
basic controlled example
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2016-01-07 19:05:55 +08:00
|
|
|
import { Tree } from 'antd';
|
|
|
|
const TreeNode = Tree.TreeNode;
|
|
|
|
|
|
|
|
const x = 3;
|
|
|
|
const y = 2;
|
|
|
|
const z = 1;
|
|
|
|
const gData = [];
|
|
|
|
|
|
|
|
const generateData = (_level, _preKey, _tns) => {
|
|
|
|
const preKey = _preKey || '0';
|
|
|
|
const tns = _tns || gData;
|
|
|
|
|
|
|
|
const children = [];
|
|
|
|
for (let i = 0; i < x; i++) {
|
|
|
|
const key = `${preKey}-${i}`;
|
2016-02-17 15:57:33 +08:00
|
|
|
tns.push({ title: key, key });
|
2016-01-07 19:05:55 +08:00
|
|
|
if (i < y) {
|
|
|
|
children.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_level < 0) {
|
|
|
|
return tns;
|
|
|
|
}
|
2016-04-29 12:13:27 +08:00
|
|
|
const level = _level - 1;
|
2016-01-07 19:05:55 +08:00
|
|
|
children.forEach((key, index) => {
|
|
|
|
tns[index].children = [];
|
2016-04-29 12:13:27 +08:00
|
|
|
return generateData(level, key, tns[index].children);
|
2016-01-07 19:05:55 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
generateData(z);
|
|
|
|
|
|
|
|
const Demo = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
2016-04-21 13:11:55 +08:00
|
|
|
expandedKeys: ['0-0-0', '0-0-1'],
|
|
|
|
autoExpandParent: true,
|
2016-01-07 19:05:55 +08:00
|
|
|
checkedKeys: ['0-0-0'],
|
|
|
|
selectedKeys: [],
|
|
|
|
};
|
|
|
|
},
|
2016-04-21 13:11:55 +08:00
|
|
|
onExpand(expandedKeys) {
|
|
|
|
console.log('onExpand', arguments);
|
|
|
|
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
|
2016-09-01 18:12:12 +08:00
|
|
|
// or, you can remove all expanded children keys.
|
2016-04-21 13:11:55 +08:00
|
|
|
this.setState({
|
|
|
|
expandedKeys,
|
|
|
|
autoExpandParent: false,
|
|
|
|
});
|
2016-01-07 19:05:55 +08:00
|
|
|
},
|
2016-01-15 20:10:46 +08:00
|
|
|
onCheck(checkedKeys) {
|
2016-01-07 19:05:55 +08:00
|
|
|
this.setState({
|
2016-01-15 20:10:46 +08:00
|
|
|
checkedKeys,
|
2016-01-07 19:05:55 +08:00
|
|
|
selectedKeys: ['0-3', '0-4'],
|
|
|
|
});
|
|
|
|
},
|
2016-01-15 20:10:46 +08:00
|
|
|
onSelect(selectedKeys, info) {
|
|
|
|
console.log('onSelect', info);
|
2016-01-28 15:13:17 +08:00
|
|
|
this.setState({ selectedKeys });
|
2016-01-07 19:05:55 +08:00
|
|
|
},
|
|
|
|
render() {
|
2016-01-28 15:13:17 +08:00
|
|
|
const loop = data => data.map((item) => {
|
|
|
|
if (item.children) {
|
|
|
|
return (
|
|
|
|
<TreeNode key={item.key} title={item.key} disableCheckbox={item.key === '0-0-0'}>
|
2016-01-15 20:10:46 +08:00
|
|
|
{loop(item.children)}
|
2016-01-28 15:13:17 +08:00
|
|
|
</TreeNode>
|
|
|
|
);
|
|
|
|
}
|
2016-02-22 10:52:30 +08:00
|
|
|
return <TreeNode key={item.key} title={item.key} />;
|
2016-01-28 15:13:17 +08:00
|
|
|
});
|
|
|
|
return (
|
2016-07-13 16:08:10 +08:00
|
|
|
<Tree
|
|
|
|
checkable
|
2016-01-08 14:41:05 +08:00
|
|
|
onExpand={this.onExpand} expandedKeys={this.state.expandedKeys}
|
2016-04-21 13:11:55 +08:00
|
|
|
autoExpandParent={this.state.autoExpandParent}
|
2016-01-08 14:41:05 +08:00
|
|
|
onCheck={this.onCheck} checkedKeys={this.state.checkedKeys}
|
2016-06-06 13:54:10 +08:00
|
|
|
onSelect={this.onSelect} selectedKeys={this.state.selectedKeys}
|
|
|
|
>
|
2016-01-07 19:05:55 +08:00
|
|
|
{loop(gData)}
|
|
|
|
</Tree>
|
2016-01-28 15:13:17 +08:00
|
|
|
);
|
2016-01-07 19:05:55 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
````
|