Populate Aggregate Based 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 the data in a complex format, such as with a SubGroup type where the entire data will be a sub group on the basis of a specific field value. And as per the criteria, it will also display the total number of items and also some aggregate values in the sub group footer.  
 
Suppose we want to populate a product details grid, where we want to group the product data as per the product category and each category must display a total number of products and total prices in the group footer section. 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 Product as below:
  1.   public class Product  
  2.   {  
  3.         public string ProductName { getset; }  
  4.         public decimal UnitPrice { getset; }  
  5.         public int OrderUnit { getset; }  
  6.         public int StockUnit { getset; }  
  7.         public string ProductType { getset; }  
  8.   }   
Now, we add a controller class to the project and add the following code to populate product 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 AggregatesGrid()    
  2. {    
  3.     List<Product> lstData = GetProductList();    
  4.     return Json(lstData, JsonRequestBehavior.AllowGet);    
  5. }    
  6.   
  7. public List<Product> GetProductList()    
  8. {    
  9.     List<Product> lstData = new List<Product>();    
  10.     Product objData = new Product();    
  11.     objData.ProductName = "Samsung Note 3";    
  12.     objData.UnitPrice = 32000;    
  13.     objData.OrderUnit = 10;    
  14.     objData.StockUnit = 12;    
  15.     objData.ProductType = "Mobile";    
  16.     lstData.Add(objData);    
  17.   
  18.     objData = new Product();    
  19.     objData.ProductName = "Nokia Lumia";    
  20.     objData.UnitPrice = 14000;    
  21.     objData.OrderUnit = 6;    
  22.     objData.StockUnit = 2;    
  23.     objData.ProductType = "Mobile";    
  24.     lstData.Add(objData);    
  25.   
  26.     objData = new Product();    
  27.     objData.ProductName = "Samsung Galaxy";    
  28.     objData.UnitPrice = 18000;    
  29.     objData.OrderUnit = 3;    
  30.     objData.StockUnit = 12;    
  31.     objData.ProductType = "Mobile";    
  32.     lstData.Add(objData);    
  33.   
  34.     objData = new Product();    
  35.     objData.ProductName = "Sony LED";    
  36.     objData.UnitPrice = 24000;    
  37.     objData.OrderUnit = 4;    
  38.     objData.StockUnit = 6;    
  39.     objData.ProductType = "TV";    
  40.     lstData.Add(objData);    
  41.   
  42.     objData = new Product();    
  43.     objData.ProductName = "Samsung LED";    
  44.     objData.UnitPrice = 28500;    
  45.     objData.OrderUnit = 10;    
  46.     objData.StockUnit = 12;    
  47.     objData.ProductType = "TV";    
  48.     lstData.Add(objData);    
  49.     return lstData;    
  50. }    
  51.   
  52. public ActionResult ProductGrid()    
  53. {    
  54.     return View();    
  55. }  
Now, right on the Action Method "ProductGrid" and select Add View. Open the view and write down the following code within the view: 
  1. @{  
  2.     ViewBag.Title = "ProductGrid";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.   
  6. <h2>Product Grid</h2>  
  7. <div>  
  8.     <div id="grdProduct"></div>  
  9. </div>  
  10.   
  11. <script type="text/javascript">  
  12.     $(document).ready(function () {  
  13.         $.post('@Url.Action("AggregatesGrid", "Grid")')  
  14.             .done(function (result) {  
  15.                 $('#grdProduct').kendoGrid({  
  16.                     dataSource: {  
  17.                         data: result,  
  18.                         scheme: {  
  19.                             model: {  
  20.                                 fields: {  
  21.                                     ProductName: { type: "string" },  
  22.                                     UnitPrice: { type: "number" },  
  23.                                     OrderUnit: { type: "number" },  
  24.                                     StockUnit: { type: "number" }  
  25.                                 }  
  26.                             }  
  27.                         },  
  28.                         group: {  
  29.                             field: "ProductType", aggregates: [  
  30.                                { field: "ProductName", aggregate: "count" },  
  31.                                { field: "UnitPrice", aggregate: "sum" },  
  32.                                { field: "UnitsOnOrder", aggregate: "average" },  
  33.                                { field: "UnitsInStock", aggregate: "count" }  
  34.                             ]  
  35.                         },  
  36.                         aggregate: [  
  37.                                         { field: "ProductName", aggregate: "count" },  
  38.                                         { field: "UnitPrice", aggregate: "sum" },  
  39.                                        { field: "UnitsOnOrder", aggregate: "average" },  
  40.                                        { field: "UnitsInStock", aggregate: "count" }  
  41.                         ]  
  42.                     },  
  43.                     sortable: true,  
  44.                     scrollable: false,  
  45.                     pageable: true,  
  46.                     columns: [  
  47.                         { field: "ProductName", title: "Product Name", aggregates: ["count"], groupFooterTemplate: "Count: #=count#" },  
  48.                         { field: "UnitPrice", title: "Unit Price", aggregates: ["sum"], groupFooterTemplate: "Total: #=sum#" },  
  49.                         { field: "OrderUnit", title: "Order Unit" },  
  50.                         { field: "StockUnit", title: "Stock Unit" }  
  51.                     ]  
  52.                 });  
  53.             }).error(function (r) {  
  54.                 alert('Try Again')  
  55.             });  
  56.     });  
  57. </script>  
 Now, its done. Now run the code and it will show the output as below:
 
 


Similar Articles