Search results

Data Binding

The TreeView component provides the option to load data either from local data sources or from remote data services. This can be done through dataSource property that is a member of the fields property. The dataSource property supports array of JavaScript objects and DataManager. It also supports different kinds of data services such as OData, OData V4, Web API, URL, and JSON with the help of DataManager adaptors.

TreeView has load on demand (Lazy load), by default. It reduces the bandwidth size when consuming huge data. It loads first level nodes initially, and when parent node is expanded, loads the child nodes based on the parentID/child member.

By default, the loadOnDemand is set to true. By disabling this property, all the tree nodes are rendered at the beginning itself.

You can use the dataBound event to perform actions. This event will be triggered once the data source is populated in the TreeView.

Local data

To bind local data to the TreeView, you can assign a JavaScript object array to the dataSource property. The TreeView component requires three fields (ID, text, and parentID) to render local data source. When mapper fields are not specified, it takes the default values as the mapping fields. Local data source can also be provided as an instance of the DataManager. It supports two kinds of local data binding methods.

  • Hierarchical data

  • Self-referential data

Hierarchical data

TreeView can be populated with hierarchical data source that contains nested array of JSON objects. You can directly assign hierarchical data to the dataSource property, and map all the field members with corresponding keys from the hierarchical data to fields property.

In the following example, code, name, and countries columns from hierarchical data have been mapped to id, text, and child fields, respectively.

razor
hierarchical-core.cs
hierarchical-mvc.cs
@Html.EJS().TreeView("listdata").Fields(ViewBag.hierarchicalFields).Render()
public IActionResult LocalData() {
            List<Continents> continents = new List<Continents>();
            List<Countries> countries = new List<Countries>();
            continents.Add(new Continents
            {
                code = "NA",
                name = "North America",  
                expanded=true,
                child = countries,
            });
                countries.Add(new Countries { code = "USA", name = "United States of America", selected=true});
                countries.Add(new Countries { code = "CUB", name = "Cuba" });
                countries.Add(new Countries { code = "MEX", name = "Mexico" });
            List<Countries> countries2 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "AF",
                name = "Africa",
                child = countries2,
            });
            countries2.Add(new Countries { code = "NGA", name = "Nygeria" });
            countries2.Add(new Countries { code = "EGY", name = "Egypt" });
            countries2.Add(new Countries { code = "ZAF", name = "South Africa" });
            List<Countries> countries3 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "AS",
                name = "Asia",
                child = countries3,
            });
            countries3.Add(new Countries { code = "CHN", name = "China" });
            countries3.Add(new Countries { code = "IND", name = "India" });
            countries3.Add(new Countries { code = "JPN", name = "Japan" });
            List<Countries> countries4 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "EU",
                name = "Europe",
                child = countries4,
            });
            countries4.Add(new Countries { code = "DNK", name = "Denmark" });
            countries4.Add(new Countries { code = "FIN", name = "Finland" });
            countries4.Add(new Countries { code = "AUT", name = "Austria" });

            List<Countries> countries5 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "SA",
                name = "South America",
                child = countries5,
            });
            countries5.Add(new Countries { code = "BRA", name = "Brazil" });
            countries5.Add(new Countries { code = "COL", name = "Colombia" });
            countries5.Add(new Countries { code = "ARG", name = "Argentina" });

            List<Countries> countries6 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "OC",
                name = "Oceania",
                child = countries6,
            });
            countries6.Add(new Countries { code = "AUS", name = "Australia" });
            countries6.Add(new Countries { code = "NZL", name = "Newzealand" });
            countries6.Add(new Countries { code = "WSM", name = "Samoa" });

            List<Countries> countries7 = new List<Countries>();
            continents.Add(new Continents
            {
                code = "AN",
                name = "Antartica",
                child = countries7,
            });
            countries7.Add(new Countries { code = "BVT", name = "Bouvet Island" });
            countries7.Add(new Countries { code = "ATF", name = "French Southern Lands" });

            char[] value = { 'c', 'h', 'i', 'l', 'd' };
            string Child = new string(value);
            ViewBag.child = Child;
            ViewBag.data = continents;
            return View();
          
        }
    }
    public class Continents
    {
        public string code;
        public string name;
        public bool expanded;
        public bool selected;
        public List<Countries> child;

    }
    public class Countries
    {
        public string code;
        public string name;
        public bool expanded;
        public bool selected;

    }
using Syncfusion.EJ2.Navigations;
  
 public IActionResult LocalData() {
            TreeViewFieldsSettings hierarchicalFields = new TreeViewFieldsSettings();
            List<TreeviewHierarchical> TreeviewHierarchical = new List<TreeviewHierarchical>();
            List<Countries1> Countries1 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "NA",
                Name = "North America",
                Expanded = true,
                Child = Countries1,
            });
            Countries1.Add(new Countries1 { Code = "USA", Name = "United States of America", Selected = true });
            Countries1.Add(new Countries1 { Code = "CUB", Name = "Cuba" });
            Countries1.Add(new Countries1 { Code = "MEX", Name = "Mexico" });
            List<Countries1> Countries2 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "AF",
                Name = "Africa",
                Child = Countries2,
            });
            Countries2.Add(new Countries1 { Code = "NGA", Name = "Nygeria" });
            Countries2.Add(new Countries1 { Code = "EGY", Name = "Egypt" });
            Countries2.Add(new Countries1 { Code = "ZAF", Name = "South Africa" });
            List<Countries1> Countries3 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "AS",
                Name = "Asia",
                Child = Countries3,
            });
            Countries3.Add(new Countries1 { Code = "CHN", Name = "China" });
            Countries3.Add(new Countries1 { Code = "IND", Name = "India" });
            Countries3.Add(new Countries1 { Code = "JPN", Name = "Japan" });
            List<Countries1> Countries4 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "EU",
                Name = "Europe",
                Child = Countries4,
            });
            Countries4.Add(new Countries1 { Code = "DNK", Name = "Denmark" });
            Countries4.Add(new Countries1 { Code = "FIN", Name = "Finland" });
            Countries4.Add(new Countries1 { Code = "AUT", Name = "Austria" });

            List<Countries1> Countries5 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "SA",
                Name = "South America",
                Child = Countries5,
            });
            Countries5.Add(new Countries1 { Code = "BRA", Name = "Brazil" });
            Countries5.Add(new Countries1 { Code = "COL", Name = "Colombia" });
            Countries5.Add(new Countries1 { Code = "ARG", Name = "Argentina" });

            List<Countries1> Countries6 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "OC",
                Name = "Oceania",
                Child = Countries6,
            });
            Countries6.Add(new Countries1 { Code = "AUS", Name = "Australia" });
            Countries6.Add(new Countries1 { Code = "NZL", Name = "Newzealand" });
            Countries6.Add(new Countries1 { Code = "WSM", Name = "Samoa" });

            List<Countries1> Countries7 = new List<Countries1>();
            TreeviewHierarchical.Add(new TreeviewHierarchical
            {
                Code = "AN",
                Name = "Antartica",
                Child = Countries7,
            });
            Countries7.Add(new Countries1 { Code = "BVT", Name = "Bouvet Island" });
            Countries7.Add(new Countries1 { Code = "ATF", Name = "French Southern Lands" });
            hierarchicalFields.DataSource = TreeviewHierarchical;
            hierarchicalFields.Expanded = "Expanded";
            hierarchicalFields.Id = "Code";
            hierarchicalFields.Selected = "Selected";
            hierarchicalFields.Text = "Name";
            hierarchicalFields.Child = "Child";
            ViewBag.hierarchicalFields = hierarchicalFields;
            return View();
        }
    public class Countries1
    {
            public string Code { get; set; }
            public string Name { get; set; }
            public bool Expanded { get; set; }
            public bool Selected { get; set; }
    }
    public class TreeviewHierarchical
    {
            public string Code { get; set; }
            public string Name { get; set; }
            public bool Expanded { get; set; }
            public List<Countries1> Child { get; set; }
    }

Self-referential data

TreeView can be populated from self-referential data structure that contains array of JSON objects with parentID mapping.

You can directly assign self-referential data to the dataSource property, and map all the field members with corresponding keys from self-referential data to fields property.

To render the root level nodes, specify the parentID as null or no need to specify the parentID in dataSource.

In the following example, id, pid, hasChild, and name columns from self-referential data have been mapped to id, parentID, hasChildren, and text fields, respectively.

razor
selfreferential-core.cs
selfreferential-mvc.cs
@Html.EJS().TreeView("listdata").Fields(ViewBag.TreeViewFields).Render()
public IActionResult LocalData()
        {

            List<object> listdata = new List<object>();
            listdata.Add(new
            {
                id = 1,
                name = "Australia",
                hasChild = true,
                expanded = true
            });
            listdata.Add(new
            {
                id = 2,
                pid = 1,
                name = "New South Wales",

            });
            listdata.Add(new
            {
                id = 3,
                pid = 1,
                name = "Victoria"
            });

            listdata.Add(new
            {
                id = 4,
                pid = 1,
                name = "South Australia"
            });
            listdata.Add(new
            {
                id = 6,
                pid = 1,
                name = "Western Australia",

            });
            listdata.Add(new
            {
                id = 7,
                name = "Brazil",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 8,
                pid = 7,
                name = "Paraná"
            });
            listdata.Add(new
            {
                id = 9,
                pid = 7,
                name = "Ceará"
            });
            listdata.Add(new
            {
                id = 10,
                pid = 7,
                name = "Acre"
            });
            listdata.Add(new
            {
                id = 11,
                name = "China",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 12,
                pid = 11,
                name = "Guangzhou"
            });
            listdata.Add(new
            {
                id = 13,
                pid = 11,
                name = "Shanghai"
            });
            listdata.Add(new
            {
                id = 14,
                pid = 11,
                name = "Beijing"
            });
            listdata.Add(new
            {
                id = 15,
                pid = 11,
                name = "Shantou"

            });
            listdata.Add(new
            {
                id = 16,
                name = "France",
                hasChild = true

            });
            listdata.Add(new
            {
                id = 17,
                pid = 16,
                name = "Pays de la Loire"

            });
            listdata.Add(new
            {
                id = 18,
                pid = 16,
                name = "Aquitaine"

            });
            listdata.Add(new
            {
                id = 19,
                pid = 16,
                name = "Brittany"

            });
            listdata.Add(new
            {
                id = 20,
                pid = 16,
                name = "Lorraine"
            });
            listdata.Add(new
            {
                id = 21,
                name = "India",
                hasChild = true

            });
            listdata.Add(new
            {
                id = 22,
                pid = 21,
                name = "Assam"

            });
            listdata.Add(new
            {
                id = 23,
                pid = 21,
                name = "Bihar"
            });
            listdata.Add(new
            {
                id = 24,
                pid = 21,
                name = "Tamil Nadu"

            });
            ViewBag.dataSource = listdata;
            return View();
        }
using Syncfusion.EJ2.Navigations;
 
 public IActionResult LocalData()
        {
            TreeViewFieldsSettings Listdata = new TreeViewFieldsSettings();
            List<object> listdata = new List<object>();
            listdata.Add(new
            {
                id = 1,
                name = "Australia",
                hasChild = true,
                expanded = true
            });
            listdata.Add(new
            {
                id = 2,
                pid = 1,
                name = "New South Wales",

            });
            listdata.Add(new
            {
                id = 3,
                pid = 1,
                name = "Victoria"
            });

            listdata.Add(new
            {
                id = 4,
                pid = 1,
                name = "South Australia"
            });
            listdata.Add(new
            {
                id = 6,
                pid = 1,
                name = "Western Australia",

            });
            listdata.Add(new
            {
                id = 7,
                name = "Brazil",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 8,
                pid = 7,
                name = "Paraná"
            });
            listdata.Add(new
            {
                id = 9,
                pid = 7,
                name = "Ceará"
            });
            listdata.Add(new
            {
                id = 10,
                pid = 7,
                name = "Acre"
            });
            listdata.Add(new
            {
                id = 11,
                name = "China",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 12,
                pid = 11,
                name = "Guangzhou"
            });
            listdata.Add(new
            {
                id = 13,
                pid = 11,
                name = "Shanghai"
            });
            listdata.Add(new
            {
                id = 14,
                pid = 11,
                name = "Beijing"
            });
            listdata.Add(new
            {
                id = 15,
                pid = 11,
                name = "Shantou"
            });
            listdata.Add(new
            {
                id = 16,
                name = "France",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 17,
                pid = 16,
                name = "Pays de la Loire"
            });
            listdata.Add(new
            {
                id = 18,
                pid = 16,
                name = "Aquitaine"

            });
            listdata.Add(new
            {
                id = 19,
                pid = 16,
                name = "Brittany"

            });
            listdata.Add(new
            {
                id = 20,
                pid = 16,
                name = "Lorraine"
            });
            listdata.Add(new
            {
                id = 21,
                name = "India",
                hasChild = true
            });
            listdata.Add(new
            {
                id = 22,
                pid = 21,
                name = "Assam"

            });
            listdata.Add(new
            {
                id = 23,
                pid = 21,
                name = "Bihar"
            });
            listdata.Add(new
            {
                id = 24,
                pid = 21,
                name = "Tamil Nadu"
            });
            Listdata.DataSource = listdata;
            Listdata.Expanded = "expanded";
            Listdata.Id = "id";
            Listdata.ParentID = "pid";
            Listdata.Text = "name";
            Listdata.HasChildren = "hasChild";
            ViewBag.TreeViewFields = Listdata;
            return View();
        }

Remote data

TreeView can also be populated from a remote data service with the help of DataManager component and Query property.

It supports different kinds of data services such as OData, OData V4, Web API, URL, and JSON with the help of DataManager adaptors.

You can assign service data as an instance of DataManager to the dataSource property. To interact with remote data source, you have to provide the endpoint url.

The DataManager that acts as an interface between the service endpoint and the TreeView requires the following information to interact with service endpoint properly.

  • DataManager->url: Defines the service endpoint to fetch data.

  • DataManager->adaptor: Defines the adaptor option. By default, ODataAdaptor is used for remote binding.

Adaptor is responsible for processing response and request from/to the service endpoint. The @syncfusion/ej2-data package provides some predefined adaptors designed to interact with service endpoints. They are,

  • UrlAdaptor: Used to interact with remote services. This is the base adaptor for all remote based adaptors.

  • ODataAdaptor: Used to interact with OData endpoints.

  • ODataV4Adaptor: Used to interact with OData V4 endpoints.

  • WebApiAdaptor: Used to interact with Web API created under OData standards.

  • WebMethodAdaptor: Used to interact with web methods.

In the following example, ODataV4Adaptor is used to fetch data from remote services. The EmployeeID, FirstName, and EmployeeID columns from Employees table have been mapped to id, text, and hasChildren fields respectively for first level nodes.

The OrderID, EmployeeID, and ShipName columns from orders table have been mapped to id, parentID, and text fields respectively for second level nodes.

razor
remotedata-core.cs
remotedata-mvc.cs
@Html.EJS().TreeView("tree").Fields(ViewBag.remoteFields).Render()
public IActionResult RemoteData()
        {
            TreeViewFieldsSettings childData = new TreeViewFieldsSettings();
            childData.Query = "new ej.data.Query().from('Orders').select('OrderID,EmployeeID,ShipName').take(5)";
            childData.Id = "OrderID";
            childData.Text = "ShipName";
            childData.ParentID = "EmployeeID";
            childData.DataSource = new DataManager
            {
                Url = "https://js.syncfusion.com/demos/ejServices/Wcf/Northwind.svc",
                Adaptor = "ODataAdaptor",
                CrossDomain = true
            };
            ViewBag.child = childData;
            return View();
        }
using Syncfusion.EJ2.Navigations;

public IActionResult RemoteData()
        {
            TreeViewFieldsSettings parentData = new TreeViewFieldsSettings();
            TreeViewFieldsSettings childData = new TreeViewFieldsSettings();
            object data = new DataManager
            {
                Url = "https://js.syncfusion.com/demos/ejServices/Wcf/Northwind.svc",
                Adaptor = "ODataAdaptor",
                CrossDomain = true
            };
            // Parent data mapping
            parentData.Query = "new ej.data.Query().from('Employees').select('EmployeeID,FirstName,Title').take(5)";
            parentData.Id = "EmployeeID";
            parentData.Text = "FirstName";
            parentData.HasChildren = "EmployeeID";
            parentData.Child = childData;
            parentData.DataSource = data;
            // Child data mapping  
            childData.Query = "new ej.data.Query().from('Orders').select('OrderID,EmployeeID,ShipName').take(5)";
            childData.Id = "OrderID";
            childData.Text = "ShipName";
            childData.ParentID = "EmployeeID";
            childData.DataSource = data;
            ViewBag.remoteFields = parentData;
            return View();
            return View();
        }