// 后台数据
[
{
"Id": "1699614694899712000",
"OrganizationName": "1",
"SuperiorOrganization": "2",
},
{
"Id": "1699614724377280511",
"OrganizationName": "2",
"SuperiorOrganization": "3",
},
{
"Id": "1699614825288040442",
"OrganizationName": "3",
},
{
"Id": "1699716853951627263",
"OrganizationName": "4",
"SuperiorOrganization": "2",
},
{
"Id": "1699726918335070204",
"OrganizationName": "5",
}, {
"Id": "1699614694899712005",
"OrganizationName": "6",
"SuperiorOrganization": "4",
},
{
"Id": "1699614724377280516",
"OrganizationName": "7",
"SuperiorOrganization": "4",
},
{
"Id": "1699614825288040447",
"OrganizationName": "8",
},
{
"Id": "1699716853951627268",
"OrganizationName": "9",
"SuperiorOrganization": "5",
},
{
"Id": "1699726918335070209",
"OrganizationName": "10",
"SuperiorOrganization": "5",
}
]
// 最终要实现的数据结构
const nodes = [
{
title: 3,
key: "1699614825288040442",
children: [
{
title: 2,
key: "1699614724377280511",
children: [
{
title: 1,
key: "1699614694899712000"
}, {
title: 4,
key: "1699716853951627263",
children: [
{
title: 6,
key: "1699614694899712005"
}, {
title: 7,
key: "1699614724377280516"
},
]
},
]
}
]
},
{
title: 5,
key: "1699726918335070204",
children: [
{
title: 9,
key: "1699716853951627268"
}, {
title: 10,
key: "1699726918335070209"
},
]
},
{
title: 8,
key: "1699614825288040447",
}
]
// 递归构建树形结构
buildTree(list, parentOrganization) {
const tree = []; // 定义空数组,存储最终的树形结构数据
for (const organization of list) { // 遍历后台list数据
if (organization.ParentOrganization === parentOrganization) { // 取含相同属性值的项,如果相同作为子进行递归
const node = {
title: `${organization.Name} [${organization.OrganizationCode}]`,
name: organization.Name,
key: organization.Id,
};
const children = this.buildTree(list, organization.Name);
if (children.length > 0) {
// @ts-ignore
node.children = children;
} else {
// @ts-ignore
node.isLeaf = true;
}
tree.push(node);
}
}
return tree;
}
this.nodes = this.buildTree(res.NgpDataSet.T_Result, undefined);