Populate Row Filter Grid Using Kendo UI and jQuery

Whenever we populate a grid to display data, it always consists of multiple rows and columns in a simple tabular format. But sometimes we need to display a large amount of data within the grid. So for this reason, we sometimes must use search criteria or filtering features within the grid so that we can easily find or filter the data on the basis of the given criteria. The said criteria or condition may be on one column or multiple columns. We can very easily do this using jQuery.
 
Suppose we want to populate a Student details grid, where  we want to provide a row filter option on the Gender and Course Columns. This article will show how to populate this type of grid view using a Kendo UI and jQuery. For this, at first we need to create the model class of Student as in the following: 
  1. public class Student  
  2. {  
  3.     public int StudentID { getset; }  
  4.     public string StudentName { getset; }  
  5.     public string CourseName { getset; }          
  6.     public string Gender { getset; }     
  7. }  
Now, we add a controller class to the project and add the following code to populate the student list. The product list will be called by an Action Method that returns a JSON result to bind the grid as follows:  
  1. public ActionResult GetStudentData()  
  2. {  
  3.      List<Student> lstData = GenerateStudentData();  
  4.      return Json(lstData, JsonRequestBehavior.AllowGet);  
  5. }  
  6.   
  7. public List<Student> GenerateStudentData()  
  8. {  
  9.      List<Student> lstData = new List<Student>();  
  10.      Student s = new Student();  
  11.      s.StudentID = 1;  
  12.      s.StudentName = "Amit";  
  13.      s.Gender = "Male";  
  14.      s.CourseName = "BCA";  
  15.      lstData.Add(s);  
  16.   
  17.      s = new Student();  
  18.      s.StudentID = 2;  
  19.      s.StudentName = "Swapan";  
  20.      s.Gender = "Male";  
  21.      s.CourseName = "B.Sc.";  
  22.      lstData.Add(s);  
  23.   
  24.      s = new Student();  
  25.      s.StudentID = 3;  
  26.      s.StudentName = "Anita";  
  27.      s.Gender = "Female";  
  28.      s.CourseName = "BCA";  
  29.      lstData.Add(s);  
  30.   
  31.      s = new Student();  
  32.      s.StudentID = 4;  
  33.      s.StudentName = "Kalpana";  
  34.      s.Gender = "Female";  
  35.      s.CourseName = "B.Sc.";  
  36.      lstData.Add(s);  
  37.   
  38.      s = new Student();  
  39.      s.StudentID = 5;  
  40.      s.StudentName = "Susweta";  
  41.      s.Gender = "Female";  
  42.      s.CourseName = "BA";  
  43.      lstData.Add(s);  
  44.   
  45.      s = new Student();  
  46.      s.StudentID = 6;  
  47.      s.StudentName = "Tirthankar";  
  48.      s.Gender = "Male";  
  49.      s.CourseName = "BCA";  
  50.      lstData.Add(s);  
  51.   
  52.      s = new Student();  
  53.      s.StudentID = 7;  
  54.      s.StudentName = "Palash";  
  55.      s.Gender = "Male";  
  56.      s.CourseName = "B.Sc.";  
  57.      lstData.Add(s);  
  58.   
  59.      s = new Student();  
  60.      s.StudentID = 8;  
  61.      s.StudentName = "Kavita";  
  62.      s.Gender = "Female";  
  63.      s.CourseName = "BCA";  
  64.      lstData.Add(s);  
  65.      s = new Student();  
  66.      s.StudentID = 9;  
  67.      s.StudentName = "Rajatabha";  
  68.      s.Gender = "Male";  
  69.      s.CourseName = "MCA";  
  70.      lstData.Add(s);  
  71.   
  72.      s = new Student();  
  73.      s.StudentID = 10;  
  74.      s.StudentName = "Dipali";  
  75.      s.Gender = "Female";  
  76.      s.CourseName = "MCA";  
  77.      lstData.Add(s);  
  78.      return lstData;  
  79. }  
  80.  
  81. public  ActionResult RowFilterGrid()  
  82. {  
  83.      return View();  
  84. }  
 Now, right-click on the Action Method "RowFilterGrid" and select Add View. Open the view and provide the following code within the view:
  1. @{  
  2.     ViewBag.Title = "Row Filter Grid";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.   
  6. <h2>Row Filter Grid</h2>  
  7. <div>  
  8.     <div id="grdProduct"></div>  
  9. </div>  
  10. <script type="text/javascript">  
  11.     $(document).ready(function () {  
  12.         $.post('@Url.Action("GetStudentData", "Grid")')  
  13.             .done(function (result) {  
  14.                 $('#grdProduct').kendoGrid({  
  15.                     dataSource: {  
  16.                         data: result,  
  17.                         scheme: {  
  18.                             model: {  
  19.                                 fields: {  
  20.                                     StudentName: { type: "string" },  
  21.                                     Gender: { type: "string" },  
  22.                                     CourseName: { type: "string" }  
  23.                                 }  
  24.                             }  
  25.                         }  
  26.                     },  
  27.                     height: 550,  
  28.                     filterable: {  
  29.                         mode: "row"  
  30.                     },  
  31.                     pageable: true,  
  32.                     columns: [{  
  33.                         field: "StudentName",  
  34.                         title: "Student Name",  
  35.                         width: 200,  
  36.                         filterable:  
  37.                             {  
  38.                                 cell:  
  39.                                     {  
  40.                                         operator"contains"  
  41.                                     }  
  42.                             }  
  43.                     },  
  44.                     {  
  45.                         field: "Gender",  
  46.                         title: "Gender",  
  47.                         width: 100,  
  48.                         filterable:  
  49.                             {  
  50.                                 cell:  
  51.                                     {  
  52.                                         operator"contains"  
  53.                                     }  
  54.                             }  
  55.                     },  
  56.                     {  
  57.                         field: "CourseName",  
  58.                         title: "Course Name",  
  59.                         width: 200,  
  60.                         filterable:  
  61.                             {  
  62.                                 cell:  
  63.                                     {  
  64.                                         operator"contains"  
  65.                                     }  
  66.                             }  
  67.                     }  
  68.                     ]  
  69.                 });  
  70.             }).error(function (r) {  
  71.                 alert('Try Again')  
  72.             });  
  73.     });  
  74. </script>  
Now, its done. Now run the code and it will show the output as in the following:
 


Similar Articles