fix 完善handleTree方法
This commit is contained in:
parent
9deae50ccc
commit
68648f2a86
@ -30,54 +30,57 @@ export function getUpFileUrl(url:string){
|
|||||||
* @param {*} rootId 根Id 默认 0
|
* @param {*} rootId 根Id 默认 0
|
||||||
*/
|
*/
|
||||||
export function handleTree(data:any[], id:string, parentId:string, children:string, rootId:any):any[] {
|
export function handleTree(data:any[], id:string, parentId:string, children:string, rootId:any):any[] {
|
||||||
id = id || 'id'
|
id = id || 'id'
|
||||||
parentId = parentId || 'parentId'
|
parentId = parentId || 'parentId'
|
||||||
children = children || 'children'
|
children = children || 'children'
|
||||||
let rootIds:any = []
|
let rootIds:any = []
|
||||||
if(typeof rootId === 'boolean' && rootId){
|
if(typeof rootId === 'boolean' && rootId){
|
||||||
//自动获取rootId
|
//自动获取rootId
|
||||||
let idSet:any = {}
|
let idSet:any = {}
|
||||||
data.map((item:any)=>{
|
data.map((item:any)=>{
|
||||||
idSet[item[id]] = true
|
idSet[item[id]] = true
|
||||||
})
|
})
|
||||||
data.map((item:any)=>{
|
data.map((item:any)=>{
|
||||||
if(!idSet[item[parentId]]){
|
if(!idSet[item[parentId]]){
|
||||||
rootIds.push(item[parentId])
|
rootIds.push(item[parentId])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}else{
|
}else if(typeof rootId ==='string'){
|
||||||
rootId = rootId || 0
|
rootId = rootId || ''
|
||||||
rootIds = [rootId]
|
rootIds = [rootId]
|
||||||
}
|
}else{
|
||||||
rootIds = [...new Set(rootIds)]
|
rootId = rootId || 0
|
||||||
let treeData:any = []
|
rootIds = [rootId]
|
||||||
//对源数据深度克隆
|
}
|
||||||
const cloneData = JSON.parse(JSON.stringify(data))
|
rootIds = [...new Set(rootIds)]
|
||||||
rootIds.map((rItem:any)=>{
|
let treeData:any = []
|
||||||
//循环所有项
|
//对源数据深度克隆
|
||||||
const td = cloneData.filter((father:any) => {
|
const cloneData = JSON.parse(JSON.stringify(data))
|
||||||
let branchArr = cloneData.filter((child:any) => {
|
rootIds.map((rItem:any)=>{
|
||||||
//返回每一项的子级数组
|
//循环所有项
|
||||||
return father[id] === child[parentId]
|
const td = cloneData.filter((father:any) => {
|
||||||
});
|
let branchArr = cloneData.filter((child:any) => {
|
||||||
branchArr.length > 0 ? father[children] = branchArr : '';
|
//返回每一项的子级数组
|
||||||
//返回第一层
|
return father[id] === child[parentId]
|
||||||
switch (typeof father[parentId]){
|
});
|
||||||
case 'string':
|
branchArr.length > 0 ? father[children] = branchArr : '';
|
||||||
if(father[parentId]===''&&rItem===0){
|
//返回第一层
|
||||||
return true
|
switch (typeof father[parentId]){
|
||||||
}
|
case 'string':
|
||||||
return father[parentId]===rItem.toString();
|
if(father[parentId]===''&&rItem===0){
|
||||||
case 'number':
|
return true
|
||||||
return father[parentId] === rItem;
|
}
|
||||||
}
|
return father[parentId]===rItem.toString();
|
||||||
return false;
|
case 'number':
|
||||||
});
|
return father[parentId] === rItem;
|
||||||
if(td.length>0){
|
}
|
||||||
treeData = [...treeData,...td]
|
return false;
|
||||||
}
|
});
|
||||||
})
|
if(td.length>0){
|
||||||
return treeData != '' ? treeData : data;
|
treeData = [...treeData,...td]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return treeData != '' ? treeData : data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenTree(treeArray:any[]):any[] {
|
export function flattenTree(treeArray:any[]):any[] {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user