ant-design-vue/components/vc-tree/demo/basic-controlled.vue

182 lines
5.0 KiB
Vue
Raw Normal View History

2018-04-11 13:25:16 +00:00
<script>
/* eslint no-console:0 */
2019-01-12 03:33:27 +00:00
import Tree, { TreeNode } from '../index';
import '../assets/index.less';
import { gData, getRadioSelectKeys } from './util';
import '../../vc-dialog/assets/index.less';
import Modal from '../../vc-dialog';
import BaseMixin from '../../_util/BaseMixin';
2018-04-11 13:25:16 +00:00
export default {
mixins: [BaseMixin],
2019-09-28 12:45:07 +00:00
data() {
2018-04-11 13:25:16 +00:00
return {
// expandedKeys: getFilterExpandedKeys(gData, ['0-0-0-key']),
expandedKeys: ['0-0-0-key'],
autoExpandParent: true,
// checkedKeys: ['0-0-0-0-key', '0-0-1-0-key', '0-1-0-0-key'],
checkedKeys: ['0-0-0-key'],
checkStrictlyKeys: { checked: ['0-0-1-key'], halfChecked: [] },
selectedKeys: [],
treeData: [],
visible: false,
multiple: true,
2019-01-12 03:33:27 +00:00
};
2018-04-11 13:25:16 +00:00
},
methods: {
2019-09-28 12:45:07 +00:00
onExpand(expandedKeys) {
2019-01-12 03:33:27 +00:00
console.log('onExpand', arguments);
2018-04-11 13:25:16 +00:00
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
// or, you can remove all expanded chilren keys.
this.setState({
expandedKeys,
autoExpandParent: false,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
onCheck(checkedKeys) {
2018-04-11 13:25:16 +00:00
this.setState({
checkedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
onCheckStrictly(checkedKeys /* extra*/) {
2019-01-12 03:33:27 +00:00
console.log(arguments);
2018-04-11 13:25:16 +00:00
// const { checkedNodesPositions } = extra;
// const pps = filterParentPosition(checkedNodesPositions.map(i => i.pos));
// console.log(checkedNodesPositions.filter(i => pps.indexOf(i.pos) > -1).map(i => i.node.key));
const cks = {
checked: checkedKeys.checked || checkedKeys,
halfChecked: [`0-0-${parseInt(Math.random() * 3, 10)}-key`],
2019-01-12 03:33:27 +00:00
};
2018-04-11 13:25:16 +00:00
this.setState({
2019-09-28 12:45:07 +00:00
// checkedKeys,
2018-04-11 13:25:16 +00:00
checkStrictlyKeys: cks,
2019-09-28 12:45:07 +00:00
// checkStrictlyKeys: checkedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
onSelect(selectedKeys, info) {
2019-01-12 03:33:27 +00:00
console.log('onSelect', selectedKeys, info);
2018-04-11 13:25:16 +00:00
this.setState({
selectedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
onRbSelect(selectedKeys, info) {
2019-01-12 03:33:27 +00:00
let _selectedKeys = selectedKeys;
2018-04-11 13:25:16 +00:00
if (info.selected) {
2019-01-12 03:33:27 +00:00
_selectedKeys = getRadioSelectKeys(gData, selectedKeys, info.node.eventKey);
2018-04-11 13:25:16 +00:00
}
this.setState({
selectedKeys: _selectedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
onClose() {
2018-04-11 13:25:16 +00:00
this.setState({
visible: false,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
handleOk() {
2018-04-11 13:25:16 +00:00
this.setState({
visible: false,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
showModal() {
2018-04-11 13:25:16 +00:00
this.setState({
expandedKeys: ['0-0-0-key', '0-0-1-key'],
checkedKeys: ['0-0-0-key'],
visible: true,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
// simulate Ajax
setTimeout(() => {
this.setState({
treeData: [...gData],
2019-01-12 03:33:27 +00:00
});
}, 2000);
2018-04-11 13:25:16 +00:00
},
2019-09-28 12:45:07 +00:00
triggerChecked() {
2018-04-11 13:25:16 +00:00
this.setState({
checkedKeys: [`0-0-${parseInt(Math.random() * 3, 10)}-key`],
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
},
2019-09-28 12:45:07 +00:00
render() {
2018-04-11 13:25:16 +00:00
const loop = data => {
2019-09-28 12:45:07 +00:00
return data.map(item => {
2018-04-11 13:25:16 +00:00
if (item.children) {
return (
2019-09-28 12:45:07 +00:00
<TreeNode key={item.key} title={item.title} disableCheckbox={item.key === '0-0-0-key'}>
2018-04-11 13:25:16 +00:00
{loop(item.children)}
</TreeNode>
2019-01-12 03:33:27 +00:00
);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
return <TreeNode key={item.key} title={item.title} />;
});
};
2018-04-11 13:25:16 +00:00
// console.log(getRadioSelectKeys(gData, this.selectedKeys));
2019-09-28 12:45:07 +00:00
return (
<div style={{ padding: '0 20px' }}>
<h2>dialog</h2>
<button class="btn btn-primary" onClick={this.showModal}>
show dialog
</button>
<Modal title="TestDemo" visible={this.visible} onOk={this.handleOk} onClose={this.onClose}>
{this.treeData.length ? (
<Tree
checkable
class="dialog-tree"
onExpand={this.onExpand}
expandedKeys={this.expandedKeys}
autoExpandParent={this.autoExpandParent}
onCheck={this.onCheck}
checkedKeys={this.checkedKeys}
>
{loop(this.treeData)}
</Tree>
) : (
'loading...'
)}
</Modal>
2018-04-11 13:25:16 +00:00
2019-09-28 12:45:07 +00:00
<h2>controlled</h2>
<Tree
checkable
onExpand={this.onExpand}
expandedKeys={this.expandedKeys}
autoExpandParent={this.autoExpandParent}
onCheck={this.onCheck}
checkedKeys={this.checkedKeys}
onSelect={this.onSelect}
selectedKeys={this.selectedKeys}
>
{loop(gData)}
</Tree>
<button onClick={this.triggerChecked}>trigger checked</button>
2018-04-11 13:25:16 +00:00
2019-09-28 12:45:07 +00:00
<h2>checkStrictly</h2>
<Tree
checkable
multiple={this.multiple}
defaultExpandAll
onExpand={this.onExpand}
expandedKeys={this.expandedKeys}
onCheck={this.onCheckStrictly}
checkedKeys={this.checkStrictlyKeys}
checkStrictly
>
{loop(gData)}
</Tree>
2018-04-11 13:25:16 +00:00
2019-09-28 12:45:07 +00:00
<h2>radio's behavior select (in the same level)</h2>
<Tree
multiple
defaultExpandAll
onSelect={this.onRbSelect}
selectedKeys={getRadioSelectKeys(gData, this.selectedKeys)}
>
{loop(gData)}
</Tree>
</div>
);
2018-04-11 13:25:16 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-04-11 13:25:16 +00:00
</script>