嗨,我正在Angular 5中开发Web应用程序。我正在Web应用程序中显示树结构。我已经下载了一些示例代码,效果很好。下面是我的代码。
asyncChildren = [
{
name: 'child1',
hasChildren: true
}, {
name: 'child2'
}
];
constructor(public nodeservice:NodeService) {
this.nodes = [
{
name: 'root1',
children: [
{ name: 'child1' }
]
},
{
name: 'root2',
hasChildren: true
},
{
name: 'root3'
}
];
}
getChildren(node: any) {
const newNodes = this.asyncChildren.map((c) => Object.assign({}, c));
console.log(newNodes);
return new Promise((resolve, reject) => {
setTimeout(() => resolve(newNodes), 1000);
});
}
在上面的代码中,当只有一个子节点时,我只显示
儿童:[ {名称:'child1'} ]
然后如果有元素数组,那么我正在显示
{ 名称:“ root2”, hasChildren:是 }
每当用户单击此getChildren函数时,就会调用asyncChildren数据,其余数据将连续添加。我的api响应格式不同。因此,我计划编写某种翻译,以便可以以树格式显示任何数据。我的示例回复是
[
{
"applicationid":"8338bd8f-653a-4b0e-9fb6-b27a1301d69b",
"objectid":"5ca3c41c-46e3-4d6e-a72b-f07b05f47388",
"tenantid":"b52f829b-a44a-4f6a-9de2-95a856115460",
"applicationname":"test",
"isactive":true,
"isdeleted":false,
"applicationroles":[
{
"applicationroleid":"a6538a9a-2941-4b5f-b433-7f0c16c46944",
"applicationid":"8338bd8f-653a-4b0e-9fb6-b27a1301d69b",
"roleid":"9732bda1-7f59-4420-9c04-4919f6f0baa5",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"9732bda1-7f59-4420-9c04-4919f6f0baa5",
"rolename":"SecurityAdmin",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopes":null,
"actionids":null,
"actions":null
}
},
{
"applicationroleid":"a925495f-4f16-4955-810f-d82417c79b6c",
"applicationid":"8338bd8f-653a-4b0e-9fb6-b27a1301d69b",
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"isactive":true,
"isdeleted":false,
"role":{
"roleid":"ada09fb2-fa83-4e46-8878-7e4e48c73111",
"rolename":"Installer",
"tenantid":"99999999-9999-9999-9999-999999999999",
"isactive":true,
"isdeleted":false,
"actionnames":null,
"scopes":null,
"actionids":null,
"actions":null
}
}
]
}
]
我开始按如下方式编写代码,并一开始就陷入困境。
public string transformer(data:any){
data.forEach(element => {
// transformation
});
}
有人可以在逻辑上帮助我!我会尝试写。我在努力做出逻辑。任何帮助,将不胜感激。谢谢