|
@@ -263,6 +263,21 @@ function onTreeNodeSelect(key,{selectedNodes}) {
|
|
toggleExpandAll(true);
|
|
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) {
|
|
function getNodeAllKey(node: any, children: any, key: string) {
|
|
const result: any = [];
|
|
const result: any = [];
|
|
result.push(node[key]);
|
|
result.push(node[key]);
|
|
@@ -289,15 +304,14 @@ function removeMatchingItems(arr1, arr2) {
|
|
|
|
|
|
// tree勾选复选框事件
|
|
// tree勾选复选框事件
|
|
function onCheck(o, e) {
|
|
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');
|
|
const keys = getNodeAllKey(e.node, 'children', 'key');
|
|
if (e.checked) {
|
|
if (e.checked) {
|
|
// 反复操作下可能会有重复的keys,得用new Set去重下
|
|
// 反复操作下可能会有重复的keys,得用new Set去重下
|
|
- checkedKeys.value = [...new Set([...checkedKeys.value, ...keys])];
|
|
|
|
|
|
+ checkedKeys.value = [...new Set([...checkedKeys.value, ...keys,...ancestorsKeys])];
|
|
} else {
|
|
} else {
|
|
const result = removeMatchingItems(checkedKeys.value, keys);
|
|
const result = removeMatchingItems(checkedKeys.value, keys);
|
|
checkedKeys.value = result;
|
|
checkedKeys.value = result;
|