diff --git a/src/components/formTable/index.vue b/src/components/formTable/index.vue index d82288c..75c292e 100644 --- a/src/components/formTable/index.vue +++ b/src/components/formTable/index.vue @@ -1958,13 +1958,26 @@ function getTree1() { } - - function modifyTreeData(treeData, idList) { - console.log("modifyTreeDataMinimal 执行了"); - + //console.log("modifyTreeDataMinimal 执行了"); + let gkFlag = true const idArray = Array.isArray(idList) ? idList : [idList]; - if (idArray.length === 0 || !treeData) { + + // 当 gkFlag 为 true 且 idList 为空时,查找特定节点作为新的 treeData + if (gkFlag && idArray.length === 0) { + const targetNode = findTargetNode(treeData); + if (targetNode) { + // 使用找到的节点作为新的 treeData + treeData = targetNode; + } + } + + // 当 idList 为空时,使用相同的处理逻辑,但保持所有节点 + if (idArray.length === 0) { + return processTreeWithAllNodes(treeData); + } + + if (!treeData) { return Array.isArray(treeData) ? [] : null; } @@ -1981,13 +1994,16 @@ function modifyTreeData(treeData, idList) { const node = stack.pop(); if (!node?.id) continue; - nodeMap.set(node.id, node); - - if (node.children) { - for (const child of node.children) { - if (child?.id) { - parentMap.set(child.id, node.id); - stack.push(child); + // 如果节点ID已存在,跳过重复节点 + if (!nodeMap.has(node.id)) { + nodeMap.set(node.id, node); + + if (node.children) { + for (const child of node.children) { + if (child?.id) { + parentMap.set(child.id, node.id); + stack.push(child); + } } } } @@ -2026,6 +2042,29 @@ function modifyTreeData(treeData, idList) { return Array.isArray(treeData) ? result : result[0] || null; } +// 辅助函数:查找特定条件的节点 +function findTargetNode(treeData) { + if (!treeData) return null; + + const stack = Array.isArray(treeData) ? [...treeData] : [treeData]; + + while (stack.length > 0) { + const node = stack.pop(); + + // 检查节点是否符合条件 + if (node.id === "309" && node.label === "山东恒信高科能源有限公司") { + return node; + } + + // 继续遍历子节点 + if (node.children && node.children.length > 0) { + stack.push(...node.children); + } + } + + return null; +} + // 辅助函数:递归构建节点树 function buildNodeTree(node, keepIds, idArray, processedIds) { const newNode = { @@ -2043,9 +2082,11 @@ function buildNodeTree(node, keepIds, idArray, processedIds) { // 处理子节点 if (node.children && node.children.length > 0) { const children = []; + const childProcessedIds = new Set(); // 用于子节点的去重 + for (const child of node.children) { - if (child?.id && keepIds.has(child.id) && !processedIds.has(child.id)) { - processedIds.add(child.id); + if (child?.id && keepIds.has(child.id) && !childProcessedIds.has(child.id)) { + childProcessedIds.add(child.id); children.push(buildNodeTree(child, keepIds, idArray, processedIds)); } } @@ -2057,6 +2098,84 @@ function buildNodeTree(node, keepIds, idArray, processedIds) { return newNode; } +// 当 idList 为空时处理树数据,使用与不为空时相同的格式化逻辑 +function processTreeWithAllNodes(treeData) { + if (!treeData) { + return Array.isArray(treeData) ? [] : null; + } + + const roots = Array.isArray(treeData) ? treeData : [treeData]; + + // 构建结果树 - 使用与不为空时相同的逻辑,但应用到所有节点 + const result = []; + const processedIds = new Set(); + + // 处理所有根节点 + for (const root of roots) { + if (!root?.id || processedIds.has(root.id)) { + continue; + } + + processedIds.add(root.id); + // 使用相同的格式化逻辑,但应用到所有节点,并过滤没有用户的组织节点 + const formattedNode = formatAllNodes(root, processedIds); + if (formattedNode) { + result.push(formattedNode); + } + } + + // 清理临时数据 + processedIds.clear(); + + return Array.isArray(treeData) ? result : result[0] || null; +} + +// 递归格式化所有节点,保持与非空时相同的格式,并过滤没有用户的组织节点 +function formatAllNodes(node, processedIds) { + // 判断是否为组织节点(ID长度小于5) + const isOrganization = node.id && node.id.length < 5; + + // 处理子节点 + let children = []; + const childProcessedIds = new Set(); + + if (node.children && node.children.length > 0) { + for (const child of node.children) { + if (child?.id && !childProcessedIds.has(child.id)) { + childProcessedIds.add(child.id); + const formattedChild = formatAllNodes(child, processedIds); + if (formattedChild) { + children.push(formattedChild); + } + } + } + } + + // 如果是组织节点且没有用户子节点,则过滤掉 + if (isOrganization && children.length === 0) { + return null; + } + + // 对所有节点应用相同的格式化逻辑 + const newNode = { + id: node.id, + label: node.label, + value: node.value + }; + + // 对所有节点应用相同的标签格式化 + if (node.value !== null && node.value !== undefined) { + newNode.label = `${node.label}(${node.value})`; + newNode.number = newNode.label; + } + + // 如果有子节点,添加子节点 + if (children.length > 0) { + newNode.children = children; + } + + return newNode; +}