Multi-Check Filtering In Kendo Grid Using ASP.NET Web API

Introduction

This article explains how to implement multi-check filtering in Kendo Grid, using ASP.NET Web API. To explain it, I have created a RESTful GET Service that is used to load the Data Source of Kendo Grid.

Prerequisites

Basic knowledge of ASP.NET WebAPI, jQuery, and Kendo UI Grid.

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 multi-check filtering in Kendo Grid.

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 “MultiCheckFilteringGrid”.

Figure 1

Figure 2

Creating a Model

In Solution Explorer, right click the 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, string Company)  
  4.        {  
  5.            this.EmployeeID = Id;  
  6.            this.EmployeeName = Name;  
  7.            this.Designation = Designation;  
  8.            this.Company = Company;  
  9.   
  10.        }  
  11.        public int EmployeeID { getset; }  
  12.        public string EmployeeName { getset; }  
  13.        public string Designation { getset; }  
  14.        public string Company { getset; }  
  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.
 
 
Figure 3
 
EmployeeController.cs
  1. [RoutePrefix("api/Employee")]  
  2.     public class EmployeeController : ApiController  
  3.     {  
  4.         [HttpGet]  
  5.         [AllowAnonymous]  
  6.         [Route("EmployeeList")]  
  7.         public HttpResponseMessage GetEmployee()  
  8.         {  
  9.             try  
  10.             {  
  11.                 List<Employee> EmpLists = new List<Employee>();  
  12.                 EmpLists.Add(new Employee(1, "Govind Raj""Business Analyst""Company A"));  
  13.                 EmpLists.Add(new Employee(2, "Krishn Mahato""Development""Company B"));  
  14.                 EmpLists.Add(new Employee(3, "Bob Ross""Testing""Company A"));  
  15.                 EmpLists.Add(new Employee(4, "Steve Davis""Development""Company A"));  
  16.                 EmpLists.Add(new Employee(5, "Dave Tucker""Infrastructure""Company B"));  
  17.                 EmpLists.Add(new Employee(6, "James Anderson""HR""Company A"));  
  18.                  
  19.                 return Request.CreateResponse(HttpStatusCode.OK, EmpLists, Configuration.Formatters.JsonFormatter);  
  20.             }  
  21.             catch (Exception ex)  
  22.             {  
  23.                 return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  24.             }  
  25.         }  
  26.     }  
The Employee Controller Action GetEmployee() will return the list of employees.
 
Testing the REST API

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

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

 
Figure 4

Yes, now our API is ready. Let's try to create a datasource for Kendo Grid by using this service.

Creating a HTML page and implementing multi select filtering in kendo Grid

Create a new HTML page in our Application. In my case, I named it as kendoGrid.html 
 
KendoGrid.html 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Kendo Grid</title>  
  6.     <meta charset="utf-8" />  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />  
  10.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />  
  11.     <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  13. </head>  
  14.   
  15. <body>  
  16.     <div id="example">  
  17.         <div id="grid"></div>  
  18.         <script>  
  19.   
  20.             $(document).ready(function() {  
  21.                 $("#grid").kendoGrid({  
  22.                     dataSource: {  
  23.                         type: "json",  
  24.                         transport: {  
  25.                             read: "/api/Employee/EmployeeList"  
  26.                         },  
  27.                         schema: {  
  28.                             model: {  
  29.                                 fields: {  
  30.                                     EmployeeID: {  
  31.                                         type: "number"  
  32.                                     },  
  33.                                     EmployeeName: {  
  34.                                         type: "string"  
  35.                                     },  
  36.                                     Designation: {  
  37.                                         type: "string"  
  38.                                     },  
  39.                                     Company: {  
  40.                                         type: "string"  
  41.                                     }  
  42.                                 }  
  43.                             }  
  44.                         },  
  45.                     },  
  46.                     filterable: true,  
  47.                     sortable: true,  
  48.                     pageable: true,  
  49.                     columns: [{  
  50.                         field: "EmployeeID",  
  51.                         filterable: false  
  52.                     }, {  
  53.                         field: "EmployeeName",  
  54.                         title: "EmployeeName",  
  55.                         filterable: { multi: true}  
  56.                     }, {  
  57.                         field: "Designation",  
  58.                         title: "Designation"  
  59.                     }, {  
  60.                         field: "Company",  
  61.                         title: "Company",  
  62.                     }]  
  63.                 });  
  64.             });  
  65.         </script>  
  66.     </div>  
  67. </body>  
  68.   
  69. </html>    
To implement the multi check filtering functionality in the Kendo Grid, first we need to enable the filtering functioanlity using the filterable property ( filterable:true). To enable the multi select option in filtering, we need to set the multi option as true (filterable :{multi:true})  
 
Result in Browser


Figure 5
 
Including the search box with multi-check in filtering.
 
kendoGrid.html 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Kendo Grid</title>  
  6.     <meta charset="utf-8" />  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />  
  10.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />  
  11.     <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  13. </head>  
  14.   
  15. <body>  
  16.     <div id="example">  
  17.         <div id="grid"></div>  
  18.         <script>  
  19.   
  20.             $(document).ready(function() {  
  21.                 $("#grid").kendoGrid({  
  22.                     dataSource: {  
  23.                         type: "json",  
  24.                         transport: {  
  25.                             read: "/api/Employee/EmployeeList"  
  26.                         },  
  27.                         schema: {  
  28.                             model: {  
  29.                                 fields: {  
  30.                                     EmployeeID: {  
  31.                                         type: "number"  
  32.                                     },  
  33.                                     EmployeeName: {  
  34.                                         type: "string"  
  35.                                     },  
  36.                                     Designation: {  
  37.                                         type: "string"  
  38.                                     },  
  39.                                     Company: {  
  40.                                         type: "string"  
  41.                                     }  
  42.                                 }  
  43.                             }  
  44.                         },  
  45.                     },  
  46.                     filterable: true,  
  47.                     sortable: true,  
  48.                     pageable: true,  
  49.                     columns: [{  
  50.                         field: "EmployeeID",  
  51.                         filterable: false  
  52.                     }, {  
  53.                         field: "EmployeeName",  
  54.                         title: "EmployeeName",  
  55.                         filterable: { multi: true, search: true }  
  56.                     }, {  
  57.                         field: "Designation",  
  58.                         title: "Designation"  
  59.                     }, {  
  60.                         field: "Company",  
  61.                         title: "Company",  
  62.                     }]  
  63.                 });  
  64.             });  
  65.         </script>  
  66.     </div>  
  67. </body>  
  68.   
  69. </html>    
Set the seacrh as true in filterable property of Kendo Grid to enable the search option in filter.
 
Result in Browser
 


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