Introduction
Kendo DropDownTree control is used to display the hierarchical data with tree view structure in DropDownList. In this article, we are going to see how to do remote binding in Kendo DropDownTree.
WEB API Services
I'm going to use the following WEB API Services which are developed using ASP.NET CORE to construct the hierarchical data source for Kendo DropDownTree control.
Employee.cs
- public class Employee
- {
- public int EmployeeId { get; set; }
- public string Name { get; set; }
- public bool HasEmployees { get; set; }
- public int? ReportsTo { get; set; }
-
- public Employee(int EmployeeId, string Name, bool HasEmployee, int? ReportsTo)
- {
- this.EmployeeId = EmployeeId;
- this.Name = Name;
- this.HasEmployees = HasEmployee;
- this.ReportsTo = ReportsTo;
- }
-
- }
EmployeeController.cs
- [Produces("application/json")]
- [Route("api/Employees")]
- public class EmployeesController : Controller
- {
-
- [HttpGet]
- [AllowAnonymous]
- [Route("Empdetails")]
- public List<Employee> GetEmployees(int? EmployeeId)
- {
- List<Employee> employees = new List<Employee>();
- employees.Add(new Employee(1, "Arun", true, null));
- employees.Add(new Employee(2, "Pradeep", false, 1));
- employees.Add(new Employee(3, "Gowtham", true, null));
- employees.Add(new Employee(4, "Raj", false, 3));
- if (EmployeeId != null)
- {
- return employees.Where(e => e.ReportsTo == EmployeeId).ToList();
- }
- else
- {
- return employees.Where(e => !e.ReportsTo.HasValue).ToList();
- }
- }
- }
GetEmployees action will return a list of employees based on Employeeid.
Testing the API in Postman
http://github-ci-staging.azurewebsites.net/api/Employees/Empdetails
http://github-ci-staging.azurewebsites.net/api/Employees/Empdetails?employeeid=1
Construct the hierarchical DataSource for kendo DropDownTree.
KendoDropDownTree.html
- <!DOCTYPE html>
- <html>
- <head>
- <base href="https://demos.telerik.com/kendo-ui/dropdowntree/remote-data-binding">
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.516/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.516/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.516/styles/kendo.material.mobile.min.css" />
-
- <script src="https://kendo.cdn.telerik.com/2018.2.516/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.2.516/js/kendo.all.min.js"></script>
-
-
- </head>
- <body>
- <h2> Kendo DropDownTree</h2>
- <br />
- <br />
- <div id="example">
- <div class="demo-section k-content">
- <input id="dropdowntree" style="width: 100%;" />
- </div>
- <script>
-
- homogeneous = new kendo.data.HierarchicalDataSource({
- transport: {
- read: {
- url: "http://github-ci-staging.azurewebsites.net/api/Employees/Empdetails",
- dataType: "json"
- }
- },
- schema: {
- model: {
- id: "employeeId",
- hasChildren: "hasEmployees"
- }
- }
- });
-
- $("#dropdowntree").kendoDropDownTree({
- placeholder: "Select ...",
- dataSource: homogeneous,
- height: "auto",
- dataTextField: "name"
- });
- </script>
- </div>
-
-
- </body>
- </html>
The dataload in the hierarchical data source will happen based on the hasChildren property which is defined in the schema. Initial request will fetch the employee details which contains hasEmployees value as true. The rest of the requests will happen based on Initial load.
Http request driven by hierarchial data source
Open the developer tool in the browser and switch to the Network section to check the HTTP request.
From the above figure, you can notice that to load the complete hierarchical data three HTTP calls are made from kendo hierarchical datasource.
The first call will load the employee details where the hasEmployees value is true, next call depends on the first call based on employeeId to load the rest of employees under Employee with employeeId as 1 and the final call will load the employees under Employee with employeeId 3. In the hierarchal data source, first, the master data will get loaded and then the child data will load to frame the tree structure.
Reference
https://docs.telerik.com/kendo-ui/controls/editors/dropdowntree/overview
I hope you have enjoyed this article. Your valuable feedback, question, or comments about this article are always welcomed.
For source code - click
here.