Kendo Hierarchy Grid

Introduction

 
Hierarchy Grid is nothing but a grid inside a grid. Like a Parent grid and child grid based on fields in them where we can filter explicitly. This article demonstrates how to bind the Hierarchy Grid dynamically using Kendo.
 
Kendo Installation
 
Kendo is the licensed one. Please look at this link for the installation.
 

jQuery Kendo Grid

 
Kendo Grid which allows us to view data in table structure. It has various features regarding the grid where we can implement easily by supplying the properties wherever you need. 
  1. <div id="exampleGrid"></div>        
  2. <script>        
  3.     $(document).ready(function () {        
  4.         var columns = [{        
  5.             field: "studentid",        
  6.             title: "Student Id",        
  7.             fieldtype: "Number"        
  8.         },{        
  9.             field: "studentname",        
  10.             title: "Student Name",        
  11.             fieldtype: "String"        
  12.         }];        
  13.         var data = [        
  14.             {studentid: 1, studentname: 'Paulraj' },        
  15.             {studentid: 2, studentname: 'Arivumani' },        
  16.             {studentid: 3, studentname: 'Viswanathan' },        
  17.             {studentid: 4, studentname: 'Bharath' },        
  18.             {studentid: 5, studentname: 'Harish' }        
  19.         ];        
  20.         var schemaModel = fnGenerateSchemaModel(columns);           
  21.         $("#exampleGrid").kendoGrid({        
  22.             selectable: "cell",        
  23.             columns: columns,        
  24.             scrollable: true,        
  25.             dataSource: {        
  26.                 data: data,        
  27.                 schema:{        
  28.                     model : schemaModel        
  29.                 }        
  30.             },        
  31.             noRecords: true,        
  32.         })        
  33.         
  34.     });        
  35.         
  36.     function fnGenerateSchemaModel(columns) {        
  37.         var model = {};        
  38.         var fields = {};        
  39.         $(columns).each(function () {        
  40.             var propType = this['fieldtype'].toLowerCase()        
  41.             fields[this['field']] = {        
  42.                 type: propType        
  43.             }        
  44.         })        
  45.         model.fields = fields;        
  46.         return model;        
  47.     }        
  48.         
  49. </script>     
Here, I have created a parent grid which has Student Id and Student Name. I am generating the schema model based on my property (fieldType) from the column object. But still, we can generate schema by grid data also.
 
 
Now the parent grid is ready. We will see how to bind the hierarchy grid dynamically.
 
Step 1
 
Add a text kendo template below the example grid div in HTML, like shown below.
  1. <div id="exampleGrid"></div>        
  2. <script type="text/x-kendo-template" id="hierarchyGridTemplate">        
  3.     <div class="hierarchy-grid"></div>        
  4. </script>   
Step 2
 
Create the child grid similar to parent grid using the hierarchyGridTemplate.
  1. function detailInit(e) {           
  2.     var detailRow = e.detailRow;        
  3.     var columns = [{          
  4.         field: "studentid",          
  5.         title: "Student Id",          
  6.         fieldtype: "Number"          
  7.     },{          
  8.         field: "tamil",          
  9.         title: "Tamil",          
  10.         fieldtype: "Number"          
  11.     },{          
  12.         field: "english",          
  13.         title: "English",          
  14.         fieldtype: "Number"          
  15.     },{          
  16.         field: "maths",          
  17.         title: "MAths",          
  18.         fieldtype: "Number"          
  19.     },{          
  20.         field: "history",          
  21.         title: "History",          
  22.         fieldtype: "Number"          
  23.     }];         
  24.         
  25.     var data = [        
  26.         {        
  27.             "studentid": 1,        
  28.             "tamil": 40,        
  29.             "english": 50,        
  30.             "maths": 65,        
  31.             "science": 56,        
  32.             "hstory": 78        
  33.         },        
  34.         {        
  35.             "studentid": 2,        
  36.             "tamil": 45,        
  37.             "english": 65,        
  38.             "maths": 67,        
  39.             "science": 78,        
  40.             "hstory": 64        
  41.         },        
  42.         {        
  43.             "studentid": 3,        
  44.             "tamil": 23,        
  45.             "english": 45,        
  46.             "maths": 78,        
  47.             "science": 99,        
  48.             "hstory": 66        
  49.         },        
  50.         {        
  51.             "studentid": 4,        
  52.             "tamil": 67,        
  53.             "english": 89,        
  54.             "maths": 53,        
  55.             "science": 97,        
  56.             "hstory": 90        
  57.         },        
  58.         {        
  59.             "studentid": 5,        
  60.             "tamil": 43,        
  61.             "english": 56,        
  62.             "maths": 78,        
  63.             "science": 97,        
  64.             "hstory": 34        
  65.         }        
  66.     ];        
  67.     var schemaModel = fnGenerateSchemaModel(columns);        
  68.     detailRow.find(".hierarchy-grid").kendoGrid({        
  69.         selectable: "cell",        
  70.         dataSource: {        
  71.             scrollable: true,        
  72.             data: data,        
  73.             schema:{        
  74.                 model : schemaModel        
  75.             },        
  76.             filter: { field: 'studentid' , operator: "eq", value: e.data['studentid'] }        
  77.         },        
  78.         columns: columns,        
  79.         noRecords: true        
  80.     });        
  81. }   
Now we have created the child grid. The highlighted part is the noticeable code here. The other is similar to creating a grid.
 
 
filter: { field: 'studentid' , operator: "eq", value: e.data['studentid'] }
 
Field is the child field name and value is the parent field data. So this is mapping to filter the hierarchy grid records.
 
In this filter, we can map whatever fields we need to filter in the child grid with the parent grid field.
 
Step 3
 
Now include the detailInit method to the parent grid dynamically by any button click or based on any condition.
  1. if(mycondition == true){        
  2.     var grid = $("#exampleGrid").data("kendoGrid");        
  3.     grid.setOptions({        
  4.         detailTemplate: kendo.template($("#hierarchyGridTemplate").html()),        
  5.         detailInit: detailInit        
  6.     });        
  7. }   
Output
 
Student Id and Student Name is the parent grid.
 
We mapped the Student Id in both grids to display the mark list of each student. Based on the data source of both grids, the result will be generated.
 
 

Summary

 
In this article, we have seen how to create a dynamic hierarchy grid in kendo.


Similar Articles