Browse Source

Merge branch 'v2.0' of eta_forum/eta_forum_admin_front into debug

leichen 2 months ago
parent
commit
cf09624da3
1 changed files with 40 additions and 29 deletions
  1. 40 29
      src/views/business_manage/components/SetPermission.vue

+ 40 - 29
src/views/business_manage/components/SetPermission.vue

@@ -81,38 +81,49 @@ const treeKeys = reactive({
     label: 'ChartClassifyName',
 });
 
-const filterNodes = (arr) => {
-  arr.length && arr.forEach((item) => {
-    // console.log(item);
-    props.allCheckedList.includes(item.ChartClassifyId) && (item.expanded = true);
-    item.Children&&item.Children.length && containsAnyValue(item.Children, props.allCheckedList) && (item.expanded = true);
-    item.Children&&item.Children.length && filterNodes(item.Children);
+// 用于存储ChartClassifyId到节点引用的映射
+const nodeMap = new Map();
+// 构建nodeIdMap的函数
+const buildNodeMap = (arr) => {
+  arr.forEach(node => {
+    nodeMap.set(node.ChartClassifyId, node);
+    if (node.Children && node.Children.length) {
+      buildNodeMap(node.Children);
+    }
   });
-  return arr;
-}
+};
+const filterNodes = (arr) => {
+  // 构建nodeIdMap
+  buildNodeMap(arr);
+  // 遍历数组并处理节点的函数
+  const processNodes = (nodes) => {
+    nodes.forEach(node => {
+      // 检查当前节点的ChartClassifyId是否在allCheckedList中
+      if (props.allCheckedList.includes(node.ChartClassifyId)) {
+        // 标记当前节点为expanded
+        node.expanded = true;
 
-const containsAnyValue = (multiDimArray, targetArray) => {
-    // 辅助函数,用于递归遍历数组
-    function recursiveCheck(subArray) {
-        for (let item of subArray) {
-            if (Array.isArray(item)) {
-                // 如果当前元素是数组,递归检查
-                if (recursiveCheck(item)) {
-                    return true;
-                }
-            } else {
-                // 检查当前元素是否在目标数组中
-                if (targetArray.includes(item.ChartClassifyId)) {
-                    return true;
-                }
-            }
+        // 标记所有父节点为expanded
+        let parentId = node.ParentId;
+        while (parentId) {
+          const parentNode = nodeMap.get(parentId);
+          if (parentNode) {
+            parentNode.expanded = true;
+          }
+          parentId = parentNode ? parentNode.ParentId : null; // 更新parentId为当前父节点的父节点ID
         }
-        // 如果没有找到任何匹配的值,返回false
-        return false;
-    }
-    // 从外层数组开始递归检查
-    return recursiveCheck(multiDimArray);
-}
+      }
+
+      // 如果当前节点有子节点,则递归处理子节点
+      if (node.Children && node.Children.length) {
+        processNodes(node.Children);
+      }
+    });
+  };
+  // 调用函数
+  processNodes(arr);
+  return arr;
+};
 
 // 监听renewalForm的变化
 watch(