From 68648f2a86b47b20b7dd2e1991a0d04cec0608b2 Mon Sep 17 00:00:00 2001 From: yxh Date: Wed, 25 Jun 2025 21:41:53 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=AE=8C=E5=96=84handleTree=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/gfast.ts | 99 ++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/src/utils/gfast.ts b/src/utils/gfast.ts index d98ad02..bff9331 100644 --- a/src/utils/gfast.ts +++ b/src/utils/gfast.ts @@ -30,54 +30,57 @@ export function getUpFileUrl(url:string){ * @param {*} rootId 根Id 默认 0 */ export function handleTree(data:any[], id:string, parentId:string, children:string, rootId:any):any[] { - id = id || 'id' - parentId = parentId || 'parentId' - children = children || 'children' - let rootIds:any = [] - if(typeof rootId === 'boolean' && rootId){ - //自动获取rootId - let idSet:any = {} - data.map((item:any)=>{ - idSet[item[id]] = true - }) - data.map((item:any)=>{ - if(!idSet[item[parentId]]){ - rootIds.push(item[parentId]) - } - }) - }else{ - rootId = rootId || 0 - rootIds = [rootId] - } - rootIds = [...new Set(rootIds)] - let treeData:any = [] - //对源数据深度克隆 - const cloneData = JSON.parse(JSON.stringify(data)) - rootIds.map((rItem:any)=>{ - //循环所有项 - const td = cloneData.filter((father:any) => { - let branchArr = cloneData.filter((child:any) => { - //返回每一项的子级数组 - return father[id] === child[parentId] - }); - branchArr.length > 0 ? father[children] = branchArr : ''; - //返回第一层 - switch (typeof father[parentId]){ - case 'string': - if(father[parentId]===''&&rItem===0){ - return true - } - return father[parentId]===rItem.toString(); - case 'number': - return father[parentId] === rItem; - } - return false; - }); - if(td.length>0){ - treeData = [...treeData,...td] - } - }) - return treeData != '' ? treeData : data; + id = id || 'id' + parentId = parentId || 'parentId' + children = children || 'children' + let rootIds:any = [] + if(typeof rootId === 'boolean' && rootId){ + //自动获取rootId + let idSet:any = {} + data.map((item:any)=>{ + idSet[item[id]] = true + }) + data.map((item:any)=>{ + if(!idSet[item[parentId]]){ + rootIds.push(item[parentId]) + } + }) + }else if(typeof rootId ==='string'){ + rootId = rootId || '' + rootIds = [rootId] + }else{ + rootId = rootId || 0 + rootIds = [rootId] + } + rootIds = [...new Set(rootIds)] + let treeData:any = [] + //对源数据深度克隆 + const cloneData = JSON.parse(JSON.stringify(data)) + rootIds.map((rItem:any)=>{ + //循环所有项 + const td = cloneData.filter((father:any) => { + let branchArr = cloneData.filter((child:any) => { + //返回每一项的子级数组 + return father[id] === child[parentId] + }); + branchArr.length > 0 ? father[children] = branchArr : ''; + //返回第一层 + switch (typeof father[parentId]){ + case 'string': + if(father[parentId]===''&&rItem===0){ + return true + } + return father[parentId]===rItem.toString(); + case 'number': + return father[parentId] === rItem; + } + return false; + }); + if(td.length>0){ + treeData = [...treeData,...td] + } + }) + return treeData != '' ? treeData : data; } export function flattenTree(treeArray:any[]):any[] {