ng2-tree icon indicating copy to clipboard operation
ng2-tree copied to clipboard

Get data form Service web api push tree not work

Open duongphamit opened this issue 9 years ago • 4 comments

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 avatar Oct 13 '17 04:10 duongphamit

@duongphamit You should have a look at https://github.com/valor-software/ng2-tree#load-children-asynchronously. Please, let me know if that helped.

rychkog avatar Oct 13 '17 06:10 rychkog

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 avatar Jan 18 '18 23:01 mmirzaeyan

@mmirzaeyan You are right, we need to document this project better, especially with real-world examples

rychkog avatar Feb 03 '18 21:02 rychkog

Use addChild method instead of push into the array, since the double quotation marks will make mistakes in TreeModel .

schuman66 avatar Apr 02 '18 07:04 schuman66