Get data form Service web api push tree not work
public tree: TreeModel = { value: 'All list', children: [] };
getList() { this.service.GeJson('Groups/GetParent').subscribe((res => { if (res) { res.forEach(element => { this.service.GeJson('Groups/getSubGroup/' + element.GroupID).subscribe((su => { let sub = su if (sub.length == 0) { this.tree.children.push({ value: element.GroupName, id: element.GroupID }) } else { var tmp = []; tmp = new Array sub.forEach(submb => { tmp.push({ value: submb.GroupName, id: submb.GroupID }) }); this.tree.children.push({ value: element.GroupName, id: element.GroupID, children: tmp }) } })) }); } })) }
@duongphamit You should have a look at https://github.com/valor-software/ng2-tree#load-children-asynchronously. Please, let me know if that helped.
i have the same problem , i think this is the most popular tree in ng2 , but there is no good documentation for working with the tree with services
i do the job like this :
`public castToTreeModel(entities: any[]): TreeModel {
let treeObject: TreeModel;
let treeData={value: 'the root node information or title' , id:-1,children:[]};
for (let element of entities){
treeData.children.push({value: element.topic, id: element.id});
}
treeObject=treeData;
return treeObject;
}
`
@mmirzaeyan You are right, we need to document this project better, especially with real-world examples
Use addChild method instead of push into the array, since the double quotation marks will make mistakes in TreeModel .