KendoTreeList View Using ASP.NET WEB API

Introduction

This article explains how to implement the KendoTreeList View, using ASP.NET Web API application. To explain it, I have created a RESTful GET Service, using ASP.NET Web API, which is used to load the DataSource of KendoTreeList.

Prerequisites

Basic knowledge of ASP.NET Web API, jQuery, and Kendo UI.

This article flows as per the following.

  1. Creating an ASP.NET Web API application.
  2. Creating a Controller.
  3. Testing the REST API.
  4. Creating an HTML page and implementing the Kendo Tree List View.

Creating an ASP.NET WEB API Application

Create a Web API application using an installed Web template in Visual Studio, as shown below. In my case, I named the application as “KendoTreeList".

 
Figure 1

  
Figure 2 

Creating Model Class

In the Solution Explorer, right click on Models folder, select "Add" followed by "Class", and name it as Employee.cs.

Employee.cs

  1. public class Employee  
  2.    {  
  3.        public Employee(int Id, string Name, string Designation,int? ReportTo )  
  4.        {  
  5.            this.EmployeeID = Id;  
  6.            this.EmployeeName = Name;  
  7.            this.Designation = Designation;  
  8.            this.ReportTo = ReportTo;  
  9.   
  10.        }  
  11.        public int EmployeeID { get; set; }  
  12.        public string EmployeeName { get; set; }  
  13.        public string Designation { get; set; }  
  14.        public int? ReportTo { get; set; }  
  15.    }  

Creating a Controller

Right click on the Controllers folder and add a new "Web API 2- Empty Controller", as shown in the Figure 3. In my case, I named it as "EmployeeController.cs". 

Figure 3 

EmployeeController.cs

  1. [HttpGet]  
  2.        [AllowAnonymous]  
  3.        [Route("EmployeeList")]  
  4.        public HttpResponseMessage GetEmployee()  
  5.        {  
  6.            try  
  7.            {  
  8.                List<Employee> EmpLists = new List<Employee>();  
  9.                EmpLists.Add(new Employee(1, "Govind Raj""Business Analyst"null));  
  10.                EmpLists.Add(new Employee(2, "Krishn Mahato""Development", 1));  
  11.                EmpLists.Add(new Employee(3, "Bob Ross""Testing", 1));  
  12.                EmpLists.Add(new Employee(4, "Steve Davis""Development", 1));  
  13.                EmpLists.Add(new Employee(5, "Dave Tucker""Infrastructure", 1));  
  14.                EmpLists.Add(new Employee(6, "James Anderson""HR", 1));  
  15.   
  16.                return Request.CreateResponse(HttpStatusCode.OK, EmpLists, Configuration.Formatters.JsonFormatter);  
  17.            }  
  18.            catch (Exception ex)  
  19.            {  
  20.                return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  21.            }  
  22.        }  

Employee Controller Action GetEmployee will return a list of the employees.

Testing the REST API

Test the API using POSTMAN/Fiddler, as shown in Figure 4.

  • API End Point /api/Employee/EmployeeList.
  • Type GET.

     
    Figure 4

Creating a HTML page

Create one new HTML page in the application, where we are going to implement the kendoTreeList view, using the RESTful Service. In my case, I named it as TreeListView.html.

Implement the KendoTreeList

The kendoTreeList view enables the display of self-referencing tabular data and allow sorting filtering and editing. We can use an empty div tags and initialize the TreeViewList settings from the script.

Write the below code in TreeListView.html.

TreeListView.html

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.       
  5.     <style>  
  6.         html {  
  7.             font-size: 14px;  
  8.             font-family: Arial, Helvetica, sans-serif;  
  9.         }  
  10.     </style>  
  11.     <title></title>  
  12.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.common-material.min.css" />  
  13.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.min.css" />  
  14.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.mobile.min.css" />  
  15.   
  16.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/jquery.min.js"></script>  
  17.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/kendo.all.min.js"></script>  
  18. </head>  
  19. <body>  
  20.     <div id="example">  
  21.         <div id="treelist"></div>  
  22.   
  23.         <script>  
  24.                 $(document).ready(function () {  
  25.   
  26.                     var dataSource = new kendo.data.TreeListDataSource({  
  27.                             transport: {  
  28.                                 read: {  
  29.                                     url: "api/Employee/EmployeeList",  
  30.                                     dataType: "json"  
  31.                                 }  
  32.                             },  
  33.                             schema: {  
  34.                                 model: {  
  35.                                     id: "EmployeeID",  
  36.                                     parentId: "ReportTo",  
  37.                                     fields: {  
  38.                                         EmployeeID: { type: "number", nullable: false },  
  39.                                         ReportTo: { field: "ReportTo", nullable: true }  
  40.                                     }  
  41.                                 }  
  42.                             }  
  43.                         });  
  44.   
  45.                     $("#treelist").kendoTreeList({  
  46.                         dataSource: dataSource,  
  47.                         columns: [  
  48.                             { field: "EmployeeName", expandable: true, title: "Employee Name", width: 250 },  
  49.                             { field: "Designation", title: "Designation" },  
  50.                               
  51.                         ]  
  52.                     });  
  53.                 });  
  54.         </script>  
  55.     </div>  
  56.   
  57.   
  58.   
  59. </body>  
  60. </html>   

More or less, the setting for both kendoGrid and kendoTreeList is the same, where in kendoTreeList View the data object will contain both Id and Parent Id to describe the hierarchy of items. From our response, the ReportTo will act as ParentId which is used to form the hierarchy.

Result
 
 
Figure 5 
 
 
Figure 6  
 
Now, let us change the value of ReportTo in the Controller to form a different hierarchy.
 
EmployeeController.cs 
  1. [HttpGet]  
  2.        [AllowAnonymous]  
  3.        [Route("EmployeeList")]  
  4.        public HttpResponseMessage GetEmployee()  
  5.        {  
  6.            try  
  7.            {  
  8.                List<Employee> EmpLists = new List<Employee>();  
  9.                EmpLists.Add(new Employee(1, "Govind Raj""Business Analyst",null));  
  10.                EmpLists.Add(new Employee(2, "Krishn Mahato""Development",1));  
  11.                EmpLists.Add(new Employee(3, "Bob Ross""Testing",1));  
  12.                EmpLists.Add(new Employee(4, "Steve Davis""Development",6));  
  13.                EmpLists.Add(new Employee(5, "Dave Tucker""Infrastructure",6));  
  14.                EmpLists.Add(new Employee(6, "James Anderson""HR",null));  
  15.                  
  16.                return Request.CreateResponse(HttpStatusCode.OK, EmpLists, Configuration.Formatters.JsonFormatter);  
  17.            }  
  18.            catch (Exception ex)  
  19.            {  
  20.                return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  21.            }  
  22.        }  
Now, the employee Krishna Mahato and Bob Ross will be under Govind Raj and Steve Davis; Dave Tucker will be under James Anderson respectively.
 
Result
 
 
Figure 7 
 
Filtering and Sorting in Kendo TreeListView  
 
Enable the filtering and sorting in the kendoTreeList View, by setting the filterable and sortable as "True".
 
TreeListView.html 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.       
  5.     <style>  
  6.         html {  
  7.             font-size: 14px;  
  8.             font-family: Arial, Helvetica, sans-serif;  
  9.         }  
  10.     </style>  
  11.     <title></title>  
  12.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.common-material.min.css" />  
  13.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.min.css" />  
  14.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.mobile.min.css" />  
  15.   
  16.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/jquery.min.js"></script>  
  17.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/kendo.all.min.js"></script>  
  18. </head>  
  19. <body>  
  20.     <h3>Kendo TreeList View using remote data </h3>  
  21.     <br />  
  22.     <br />  
  23.     <div id="example">  
  24.         <div id="treelist"></div>  
  25.   
  26.         <script>  
  27.                 $(document).ready(function () {  
  28.   
  29.                     var dataSource = new kendo.data.TreeListDataSource({  
  30.                             transport: {  
  31.                                 read: {  
  32.                                     url: "api/Employee/EmployeeList",  
  33.                                     dataType: "json"  
  34.                                 }  
  35.                             },  
  36.                             schema: {  
  37.                                 model: {  
  38.                                     id: "EmployeeID",  
  39.                                     parentId: "ReportTo",  
  40.                                     fields: {  
  41.                                         EmployeeID: { type: "number", nullable: false },  
  42.                                         ReportTo: { field: "ReportTo", nullable: true }  
  43.                                     }  
  44.                                 }  
  45.                             }  
  46.                         });  
  47.   
  48.                     $("#treelist").kendoTreeList({  
  49.                         dataSource: dataSource,  
  50.                         filterable: true,  
  51.                         sortable:true,  
  52.                         columns: [  
  53.                             { field: "EmployeeName", expandable: true, title: "Employee Name", width: 250 },  
  54.                             { field: "Designation", title: "Designation" },  
  55.                               
  56.                         ]  
  57.                     });  
  58.                 });  
  59.         </script>  
  60.     </div>  
  61.   
  62.   
  63.   
  64. </body>  
  65. </html>  
Result
 
 
Figure 8 
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all