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

173 lines
4.8 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],
data () {
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: {
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
},
onCheck (checkedKeys) {
this.setState({
checkedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +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({
// checkedKeys,
checkStrictlyKeys: cks,
// checkStrictlyKeys: checkedKeys,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +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
},
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
},
onClose () {
this.setState({
visible: false,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
handleOk () {
this.setState({
visible: false,
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
},
showModal () {
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
},
triggerChecked () {
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
},
},
render () {
const loop = data => {
return data.map((item) => {
if (item.children) {
return (
<TreeNode
key={item.key} title={item.title}
disableCheckbox={item.key === '0-0-0-key'}
>
{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));
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>
<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>
<h2>checkStrictly</h2>
<Tree
checkable multiple={this.multiple} defaultExpandAll
onExpand={this.onExpand} expandedKeys={this.expandedKeys}
onCheck={this.onCheckStrictly}
checkedKeys={this.checkStrictlyKeys}
2018-04-11 13:36:35 +00:00
checkStrictly
2018-04-11 13:25:16 +00:00
>
{loop(gData)}
</Tree>
<h2>radio's behavior select (in the same level)</h2>
<Tree
multiple defaultExpandAll
onSelect={this.onRbSelect}
selectedKeys={getRadioSelectKeys(gData, this.selectedKeys) }
>
{loop(gData)}
</Tree>
2019-01-12 03:33:27 +00:00
</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>