Data Binding in Treeview Control
26 Oct 202224 minutes to read
The TreeView control 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 control 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.
<ejs-treeview id="hierarchical">
<e-treeview-fields child="ViewBag.child" dataSource="ViewBag.data" id="code" text="name" selected="selected" expanded="expanded"></e-treeview-fields>
</ejs-treeview>
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;
}
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.
@{
.....
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"
});
<ejs-treeview id="listdata">
<e-treeview-fields dataSource="listdata" id="id" parentId="pid" text="name" hasChildren="hasChild" expanded="expanded"></e-treeview-fields>
</ejs-treeview>
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();
}
Remote data
TreeView can also be populated from a remote data service with the help of DataManager
control 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.
<ejs-treeview id="tree">
<e-treeview-fields child="ViewBag.child" query="new ej.data.Query().from('Employees').select('EmployeeID,FirstName,Title').take(5)" id="EmployeeID" text="FirstName" hasChildren="EmployeeID">
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</e-treeview-fields>
</ejs-treeview>
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://services.odata.org/V4/Northwind/Northwind.svc",
Adaptor = "ODataV4Adaptor",
CrossDomain = true
};
ViewBag.child = childData;
return View();
}