|
@@ -263,6 +263,21 @@ function onTreeNodeSelect(key,{selectedNodes}) {
|
|
|
toggleExpandAll(true);
|
|
|
}
|
|
|
|
|
|
+function findAncestors(tree, targetId, path = []) {
|
|
|
+ for (let node of tree) {
|
|
|
+ if (node.key === targetId) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ if (node.children) {
|
|
|
+ const result = findAncestors(node.children, targetId, [...path, node]);
|
|
|
+ if (result) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|
|
|
+
|
|
|
function getNodeAllKey(node: any, children: any, key: string) {
|
|
|
const result: any = [];
|
|
|
result.push(node[key]);
|
|
@@ -289,15 +304,14 @@ function removeMatchingItems(arr1, arr2) {
|
|
|
|
|
|
// tree勾选复选框事件
|
|
|
function onCheck(o, e) {
|
|
|
- // if (!Array.isArray(event)) {
|
|
|
- // checkedKeys.value = event.checked;
|
|
|
- // } else {
|
|
|
- // checkedKeys.value = event;
|
|
|
- // }
|
|
|
+ //勾选子节点时自动选择父节点与祖宗节点
|
|
|
+ const ancestors = findAncestors(treeData.value, e.node.key);
|
|
|
+ const ancestorsKeys = ancestors.map(item => item.key);
|
|
|
+ // console.log('ancestors', ancestors);
|
|
|
const keys = getNodeAllKey(e.node, 'children', 'key');
|
|
|
if (e.checked) {
|
|
|
// 反复操作下可能会有重复的keys,得用new Set去重下
|
|
|
- checkedKeys.value = [...new Set([...checkedKeys.value, ...keys])];
|
|
|
+ checkedKeys.value = [...new Set([...checkedKeys.value, ...keys,...ancestorsKeys])];
|
|
|
} else {
|
|
|
const result = removeMatchingItems(checkedKeys.value, keys);
|
|
|
checkedKeys.value = result;
|