ant-design/components/tree-select/demo/checkable.md

79 lines
1.7 KiB
Markdown
Raw Normal View History

2015-12-28 19:16:02 +08:00
# 更多功能
- order: 1
更多功能。
---
````jsx
import { TreeSelect } from 'antd';
const TreeNode = TreeSelect.TreeNode;
2015-12-31 14:38:35 +08:00
const x = 3;
const y = 2;
const z = 1;
2015-12-28 19:16:02 +08:00
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}`;
tns.push({title: key, key: key});
if (i < y) {
children.push(key);
}
}
if (_level < 0) {
return tns;
}
const __level = _level - 1;
children.forEach((key, index) => {
tns[index].children = [];
return generateData(__level, key, tns[index].children);
});
};
generateData(z);
const Demo = React.createClass({
getInitialState() {
return {
2015-12-31 14:38:35 +08:00
value: ['0-0'],
2015-12-28 19:16:02 +08:00
};
},
2015-12-31 14:38:35 +08:00
onChange(value) {
2016-01-15 20:10:46 +08:00
console.log('onChange ' + value);
2015-12-28 19:16:02 +08:00
this.setState({
2015-12-31 14:38:35 +08:00
value: value,
2015-12-28 19:16:02 +08:00
});
},
render() {
const loop = data => {
return data.map((item) => {
if (item.children) {
return <TreeNode key={item.key} value={item.key} title={item.key}>{loop(item.children)}</TreeNode>;
}
return <TreeNode key={item.key} value={item.key} title={item.key} />;
});
};
2015-12-31 14:38:35 +08:00
const tProps = {
value: this.state.value,
onChange: this.onChange,
multiple: true,
2016-01-15 20:10:46 +08:00
treeCheckable: true,
2015-12-31 14:38:35 +08:00
treeDefaultExpandAll: true,
2015-12-28 19:16:02 +08:00
};
return (<div style={{padding: '10px 30px'}}>
<h3>more</h3>
2015-12-31 14:38:35 +08:00
<TreeSelect style={{width: 300}} {...tProps}>
2015-12-28 19:16:02 +08:00
{loop(gData)}
</TreeSelect>
</div>);
},
});
2016-01-15 20:10:46 +08:00
ReactDOM.render(<Demo />, document.getElementById('components-tree-select-demo-checkable'));
2015-12-28 19:16:02 +08:00
````