Commit 3f5debd4 authored by tyn's avatar tyn

🎈 perf(根据 节点数组 重新生成直系关系树):

parent b189c987
......@@ -644,6 +644,9 @@ export function getTreeSelectAreaList(node, tree) {
// 查找到
if (nodeAncestors) {
console.log(nodeAncestors);
// 找父节点 拿到父节点的child 数量 跟 length 没有父节点 就是顶级节点
const parent = findParentNode(nodeAncestors, node.value, "value");
console.log(parent);
}
}
}
......@@ -655,6 +658,34 @@ export function getTreeSelectAreaList(node, tree) {
}
}
/**
* 根据节点 id 获取父节点
* @param {*} tree
* @param {*} targetId
* @param {*} parentNode
* @returns
*/
export function findParentNode(tree, targetId, idKey = "id", parentNode = null) {
if (tree[idKey] === targetId) {
// 找到目标节点,返回其父节点
return parentNode;
}
if (tree?.children?.length) {
for (const child of tree.children) {
const result = findParentNode(child, targetId, idKey, tree);
if (result) {
// 如果找到目标节点,返回其父节点
return result;
}
}
}
// 如果未找到目标节点,返回 null
return null;
}
/**
* 根据节点信息 找到当前节点到祖先辈组成的树形结构
* @param {*} tree
......@@ -664,7 +695,7 @@ export function getTreeSelectAreaList(node, tree) {
export function findNodeAndAncestors(tree, targetId, idKey = "id", directAncestorsOnly = true) {
if (tree[idKey] === targetId) {
// 找到目标节点,directAncestorsOnly 只找直系节点的情况下 如果是根节点(没有parent父级),返回节点本身不带子节点,否则返回节点的树形结构
return directAncestorsOnly && !tree.parent ? { ...tree, children: [] } : tree;
return directAncestorsOnly && !tree.parent ? { ...tree, children: null } : tree;
}
if (tree?.children?.length) {
......@@ -716,6 +747,66 @@ export function findNodeFromTree(tree, targetId, idKes = "id") {
return null;
}
/**
* 删除某个属性节点
* @param node
*/
export function removeAncestors(node, removeKey = "ancestors") {
delete node[removeKey];
for (const child of node.children) {
removeAncestors(child, removeKey);
}
}
/**
* 根据id 数组 生成直系关系树形结构 并排除不需要的属性
* @param tree
* @param targetIds
* @returns
*/
export function generateDirectSubtreeAndRemove(tree, targetIds) {
const findNodeById = (node, id, ancestors = []) => {
if (node.id === id) {
return { ...node, ancestors };
}
for (const child of node.children) {
const foundNode = findNodeById(child, id, [...ancestors, node]);
if (foundNode) {
return foundNode;
}
}
return null;
};
const result = { ...tree, children: [] };
for (const targetId of targetIds) {
const targetNode = findNodeById(tree, targetId);
if (targetNode) {
// Include the target node and its ancestors in the result
let currentNode = result;
for (const ancestor of targetNode.ancestors) {
const existingNode = currentNode.children.find((child) => child.id === ancestor.id);
if (existingNode) {
currentNode = existingNode;
} else {
const newNode = { ...ancestor, children: [] };
currentNode.children.push(newNode);
currentNode = newNode;
}
}
// Add the target node to the result
currentNode.children.push({ ...targetNode, children: [] });
}
}
// Remove the ancestors property from the result
removeAncestors(result);
return result.children; // Return only the direct children of the new root
}
// 甲方详情左侧菜单映射
export const detailSideBar = new Map([
// 企业速览
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment