Commit 1bad1714 authored by tyn's avatar tyn

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

parent 3f5debd4
...@@ -629,33 +629,70 @@ export function queryConditionFiltering(params) { ...@@ -629,33 +629,70 @@ export function queryConditionFiltering(params) {
* @param {Array<object>} selectList * @param {Array<object>} selectList
* @returns * @returns
*/ */
export function getTreeSelectAreaList(node, tree) { export function getTreeSelectAreaList(nodeList = [], tree, idkey = "id") {
try { 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 _result = [];
// 克隆源数据
const _tempTree = JSON.parse(JSON.stringify(tree)); const _tempTree = JSON.parse(JSON.stringify(tree));
// 根据所选节点生成tree
const newTree = generateDirectSubtreeAndRemove(nodeList, _tempTree, idkey);
if (newTree) {
// 循环找到每个节点的父节点 的选中状态
return findParentStatus(nodeList, newTree, idkey);
}
// 多树形结构 } catch (error) {
if (Object.prototype.toString.call(_tempTree) == "[object Array]") { console.log(error);
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"); export function findParentStatus(nodeList, tree, idkey) {
console.log(parent); 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;
} }
const childNodes = parent.children?.filter(childItem => nodeList.includes(childItem[idkey]));
return childNodes ? childNodes.map(childItem => childItem[idkey]) : [];
} }
} }
});
// 单树形结构 return Array.from(new Set(_temp.flat()));
} catch (error) {
console.log(error);
}
} }
...@@ -726,17 +763,17 @@ export function findNodeAndAncestors(tree, targetId, idKey = "id", directAncesto ...@@ -726,17 +763,17 @@ export function findNodeAndAncestors(tree, targetId, idKey = "id", directAncesto
* 根据节点唯一标识查找节点信息 * 根据节点唯一标识查找节点信息
* @param {*} tree * @param {*} tree
* @param {*} targetId * @param {*} targetId
* @param {*} idKes * @param {*} idKey
*/ */
export function findNodeFromTree(tree, targetId, idKes = "id") { export function findNodeFromTree(tree, targetId, ancestors = [], idKey = "id") {
if (tree[idKes] == targetId) { if (tree[idKey] == targetId) {
return tree; return { ...tree, ancestors };
} }
if (tree?.children?.length) { if (tree?.children?.length) {
for (const child of tree.children) { for (const child of tree.children) {
// 递归到孙子集查找 // 递归到孙子集查找
const result = findNodeFromTree(child, targetId); const result = findNodeFromTree(child, targetId, [...ancestors, tree], idKey);
if (result) { if (result) {
// 如果找到目标节点,返回目标节点 // 如果找到目标节点,返回目标节点
return result; return result;
...@@ -761,32 +798,24 @@ export function removeAncestors(node, removeKey = "ancestors") { ...@@ -761,32 +798,24 @@ export function removeAncestors(node, removeKey = "ancestors") {
/** /**
* 根据id 数组 生成直系关系树形结构 并排除不需要的属性 * 根据id 数组 生成直系关系树形结构 并排除不需要的属性
* @param tree * @param tree
* @param targetIds * @param targetIds []
* @returns * @returns
*/ */
export function generateDirectSubtreeAndRemove(tree, targetIds) { export function generateDirectSubtreeAndRemove(targetIds, tree, idKey = "id") {
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: [] }; const result = { ...tree, children: [] };
for (const targetId of targetIds) { for (const targetId of targetIds) {
const targetNode = findNodeById(tree, targetId);
const targetNode = findNodeFromTree(tree, targetId, [], idKey);
if (targetNode) { if (targetNode) {
// Include the target node and its ancestors in the result
// 当前节点
let currentNode = result; let currentNode = result;
for (const ancestor of targetNode.ancestors) { 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) { if (existingNode) {
currentNode = existingNode; currentNode = existingNode;
} else { } else {
...@@ -796,15 +825,15 @@ export function generateDirectSubtreeAndRemove(tree, targetIds) { ...@@ -796,15 +825,15 @@ export function generateDirectSubtreeAndRemove(tree, targetIds) {
} }
} }
// Add the target node to the result // 往目标节点追加值
currentNode.children.push({ ...targetNode, children: [] }); currentNode.children.push({ ...targetNode, children: [] });
} }
} }
// Remove the ancestors property from the result // 删除多余属性
removeAncestors(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 { ...@@ -260,10 +260,9 @@ export default {
handleChange(params) { handleChange(params) {
const selectNode = this.$refs?.areaTree?.getCheckedNodes(); const selectNode = this.$refs?.areaTree?.getCheckedNodes();
if (selectNode?.length) { if (selectNode?.length) {
const result = selectNode.map(item => { const nodeValueList = selectNode.map(item => item.value);
return getTreeSelectAreaList(item, this.areaDataList); console.log(nodeValueList);
}); console.log(getTreeSelectAreaList(nodeValueList, { childrenLength: this.areaDataList.length, children: this.areaDataList }, "value"));
console.log(result);
} }
} }
}, },
......
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