Commit 1bad1714 authored by tyn's avatar tyn

三级联动下拉 传递父级 递归处理

parent 3f5debd4
......@@ -629,33 +629,70 @@ export function queryConditionFiltering(params) {
* @param {Array<object>} selectList
* @returns
*/
export function getTreeSelectAreaList(node, tree) {
export function getTreeSelectAreaList(nodeList = [], tree, idkey = "id") {
try {
if (Object.prototype.toString.call(node) != "[object Object]") throw new Error("传入参数不是一个对象");
if (Object.prototype.toString.call(nodeList) != "[object Array]") throw new Error("传入参数不是一个节点数组");
const _result = [];
// 克隆源数据
const _tempTree = JSON.parse(JSON.stringify(tree));
// 根据所选节点生成tree
const newTree = generateDirectSubtreeAndRemove(nodeList, _tempTree, idkey);
if (newTree) {
// 循环找到每个节点的父节点 的选中状态
return findParentStatus(nodeList, newTree, idkey);
}
// 多树形结构
if (Object.prototype.toString.call(_tempTree) == "[object Array]") {
if (_tempTree.length) {
for (const iterator of _tempTree) {
const nodeAncestors = findNodeAndAncestors(iterator, node.value, "value");
// 查找到
if (nodeAncestors) {
console.log(nodeAncestors);
// 找父节点 拿到父节点的child 数量 跟 length 没有父节点 就是顶级节点
const parent = findParentNode(nodeAncestors, node.value, "value");
console.log(parent);
} catch (error) {
console.log(error);
}
}
/**
*
*/
export function findParentStatus(nodeList, tree, idkey) {
const _temp = nodeList.map(item => {
// 找节点parent
const parent = findParentNode(tree, item, idkey);
// 有parent
if (parent) {
const count = parent.childrenLength;
const len = parent.children?.length;
// 比较 count 跟 length 看子节点是否全选中
const flag = count == len && parent.children?.every(childItem => nodeList.includes(childItem[idkey])) ? true : false;
// flag为true 当前节点下的子节点全选中返回父节点id 没有则是根节点 根节点返回所有child id
if (flag) {
return parent[idkey] ? parent[idkey] : parent.children?.map(childItem => childItem[idkey]);
} else {
console.log("没有全选中");
// 没有全选中 看当前子节点是否有选中状态
const itemNode = findNodeFromTree(tree, item, [], idkey);
console.log(itemNode);
// 当前节点有子节点
if (itemNode?.children?.length) {
// 当前节点的子节点选中结果 子节点是全选状态 传递自身 否则传递子节点
const childResult = itemNode?.children?.every(childItem => nodeList.includes(childItem[idkey])) && itemNode?.children?.length == itemNode?.childrenLength;
if (childResult) {
return item;
}
const childNodes = itemNode?.children?.filter(childItem => nodeList.includes(childItem[idkey]));
return childNodes ? childNodes.map(childItem => childItem[idkey]) : [];
}
// 当前节点没有子节点 看父节点是否全选 父节点全选 返回父节点
const childResult = parent.children?.every(childItem => nodeList.includes(childItem[idkey])) && parent.children?.length == parent?.childrenLength;
if (childResult) {
return item;
}
// 单树形结构
} catch (error) {
console.log(error);
const childNodes = parent.children?.filter(childItem => nodeList.includes(childItem[idkey]));
return childNodes ? childNodes.map(childItem => childItem[idkey]) : [];
}
}
});
return Array.from(new Set(_temp.flat()));
}
......@@ -726,17 +763,17 @@ export function findNodeAndAncestors(tree, targetId, idKey = "id", directAncesto
* 根据节点唯一标识查找节点信息
* @param {*} tree
* @param {*} targetId
* @param {*} idKes
* @param {*} idKey
*/
export function findNodeFromTree(tree, targetId, idKes = "id") {
if (tree[idKes] == targetId) {
return tree;
export function findNodeFromTree(tree, targetId, ancestors = [], idKey = "id") {
if (tree[idKey] == targetId) {
return { ...tree, ancestors };
}
if (tree?.children?.length) {
for (const child of tree.children) {
// 递归到孙子集查找
const result = findNodeFromTree(child, targetId);
const result = findNodeFromTree(child, targetId, [...ancestors, tree], idKey);
if (result) {
// 如果找到目标节点,返回目标节点
return result;
......@@ -761,32 +798,24 @@ export function removeAncestors(node, removeKey = "ancestors") {
/**
* 根据id 数组 生成直系关系树形结构 并排除不需要的属性
* @param tree
* @param targetIds
* @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;
};
export function generateDirectSubtreeAndRemove(targetIds, tree, idKey = "id") {
const result = { ...tree, children: [] };
for (const targetId of targetIds) {
const targetNode = findNodeById(tree, targetId);
const targetNode = findNodeFromTree(tree, targetId, [], idKey);
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);
const existingNode = currentNode.children.find((child) => child[idKey] === ancestor[idKey]);
if (existingNode) {
currentNode = existingNode;
} else {
......@@ -796,15 +825,15 @@ export function generateDirectSubtreeAndRemove(tree, targetIds) {
}
}
// 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
return result ? JSON.parse(JSON.stringify({ ...result, children: result.children[0]?.children || [] })) : null;
}
// 甲方详情左侧菜单映射
......
......@@ -260,10 +260,9 @@ export default {
handleChange(params) {
const selectNode = this.$refs?.areaTree?.getCheckedNodes();
if (selectNode?.length) {
const result = selectNode.map(item => {
return getTreeSelectAreaList(item, this.areaDataList);
});
console.log(result);
const nodeValueList = selectNode.map(item => item.value);
console.log(nodeValueList);
console.log(getTreeSelectAreaList(nodeValueList, { childrenLength: this.areaDataList.length, children: this.areaDataList }, "value"));
}
}
},
......
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