mirror of
https://github.com/ElemeFE/element.git
synced 2025-12-19 12:04:02 +08:00
Add check event and getHalfChecedNodes method for Tree (#9730)
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user