Add check event and getHalfChecedNodes method for Tree (#9730)

This commit is contained in:
FuryBean
2018-02-09 10:31:11 +08:00
committed by 杨奕
parent 607aef0ac7
commit fe29af1b1f
7 changed files with 69 additions and 12 deletions

View File

@@ -158,7 +158,7 @@ export default class TreeStore {
const childNodes = node.root ? node.root.childNodes : node.childNodes;
childNodes.forEach((child) => {
if ((!leafOnly && child.checked) || (leafOnly && child.isLeaf && child.checked)) {
if (child.checked && (!leafOnly || (leafOnly && child.isLeaf))) {
checkedNodes.push(child.data);
}
@@ -172,17 +172,30 @@ export default class TreeStore {
}
getCheckedKeys(leafOnly = false) {
const key = this.key;
const allNodes = this._getAllNodes();
const keys = [];
allNodes.forEach((node) => {
if (!leafOnly || (leafOnly && node.isLeaf)) {
if (node.checked) {
keys.push((node.data || {})[key]);
return this.getCheckedNodes(leafOnly).map((data) => (data || {})[this.key]);
}
getHalfCheckedNodes() {
const nodes = [];
const traverse = function(node) {
const childNodes = node.root ? node.root.childNodes : node.childNodes;
childNodes.forEach((child) => {
if (child.indeterminate) {
nodes.push(child.data);
}
}
});
return keys;
traverse(child);
});
};
traverse(this);
return nodes;
}
getHalfCheckedKeys() {
return this.getHalfCheckedNodes().map((data) => (data || {})[this.key]);
}
_getAllNodes() {

View File

@@ -181,6 +181,15 @@
handleCheckChange(value, ev) {
this.node.setChecked(ev.target.checked, !this.tree.checkStrictly);
this.$nextTick(() => {
const store = this.tree.store;
this.tree.$emit('check', this.node.data, {
checkedNodes: store.getCheckedNodes(),
checkedKeys: store.getCheckedKeys(),
halfCheckedNodes: store.getHalfCheckedNodes(),
halfCheckedKeys: store.getHalfCheckedKeys(),
});
});
},
handleChildNodeExpand(nodeData, node, instance) {

View File

@@ -184,6 +184,12 @@
setChecked(data, checked, deep) {
this.store.setChecked(data, checked, deep);
},
getHalfCheckedNodes() {
return this.store.getHalfCheckedNodes();
},
getHalfCheckedKeys() {
return this.store.getHalfCheckedKeys();
},
setCurrentNode(node) {
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
this.store.setUserCurrentNode(node);