Articles in this section
Category / Section

How to load nodes from API controller in Angular TreeView?

2 mins read

The TreeView control provides support to load nodes from API controller in Angular SPA on ASP.NET Core project. This can be achieved by loading tree nodes from controller using data manager, which then assigns the returned value in the tree view data source. Refer to the following code sample.

Typescript

export class HomeComponent {
    public dataManager: any = new ej.DataManager({
           url: "/api/SampleData",
           crossDomain: true,
           adaptor: new ej.WebApiAdaptor()
 
   });
   public fields: any;
   constructor() {
           this.fields = {
               dataSource: this.dataManager,
               id: "id", text: "name", parentId: "parentId", hasChild: "hasChild"
           }
  }
}

 

Load tree data in the API controller.

C# 

public class SampleDataController : Controller {
        [HttpGet]
        public JsonResult Data()
        {
            List<treeData> treeData = new treeData().GetTreeData();
            IEnumerable<treeData> results;
            results = treeData;
            return Json(results);
        }
}
public class treeData {
        public int id { get; set; }
        public int? parentId { get; set; }
        public string name { get; set; }
        public bool? hasChild { get; set; }
        public bool? expanded { get; set; }
        public bool? ischecked { get; set; }
        public bool? selected { get; set; }
        public string spriteCss { get; set; }
        public List<treeData> GetTreeData() {
            List<treeData> data = new List<treeData>();
            data.Add(new treeData { id = 1, name = "Local Disk(C:)", hasChild = true });
            data.Add(new treeData { id = 2, name = "Local Disk(D:)", hasChild = true });
            data.Add(new treeData { id = 3, name = "Local Disk(E:)", hasChild = true });
            data.Add(new treeData { id = 4, parentId = 1, name = "Folder 1" });
            data.Add(new treeData { id = 5, parentId = 1, name = "Folder 2" });
            data.Add(new treeData { id = 6, parentId = 2, name = "Folder 3" });
            data.Add(new treeData { id = 7, parentId = 2, name = "Folder 4" });
            data.Add(new treeData { id = 8, parentId = 3, name = "Folder 5" });
            data.Add(new treeData { id = 9, parentId = 3, name = "Folder 6" });
            return data;
        }
}

 

 

Final output

TreeView Load Nodes From api controller

 

Sample: 

https://www.syncfusion.com/downloads/support/directtrac/general/ze/treeview_loadnode_ng-558284384


Conclusion

I hope you enjoyed learning about how to load nodes from API controller in Angular TreeView.

You can refer to our Angular TreeView feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our  Angular TreeView example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied