Site icon Sibeesh Passion

Implementing Nested Grid in JQWidget JQX Grid

Introduction
If you are new to JQwdget JQX Grid, I recommend you read my previous articles on the topic here.

Today we will learn how to implement a nested grid using JQWidget JQX grid files.

Background

In my previous article I said all of the properties of JQwidget JQX grid and now we can implement them. Now I have a requirement to implement the Nested grid. So I thought of sharing that with you all.

Loading required files

To start, we need to load the necessary files first. Just select the files from the Solution Explorer and drag those to the last of body section of the page.

  1. <script type=“text/javascript” src=“jqwidgets/jqxcore.js”></script>
  2. <script type=“text/javascript” src=“jqwidgets/jqxdata.js”></script>
  3. <script type=“text/javascript” src=“jqwidgets/jqxbuttons.js”></script>
  4. <script type=“text/javascript” src=“jqwidgets/jqxscrollbar.js”></script>
  5. <script type=“text/javascript” src=“jqwidgets/jqxmenu.js”></script>
  6. <script type=“text/javascript” src=“jqwidgets/jqxgrid.js”></script>
  7. <script type=“text/javascript” src=“jqwidgets/jqxgrid.selection.js”></script>
  8. <script type=“text/javascript” src=“jqwidgets/jqxgrid.filter.js”></script>
  9. <script type=“text/javascript” src=“jqwidgets/jqxgrid.sort.js”></script>

Now it is time to load the CSS style sheet for the grid.

  1. <link href=“jqwidgets/styles/jqx.base.css” rel=“stylesheet” />

Once you have loaded the required files, we can start our implementation.

Before loading the JQWidget JQX grid, we need to understand the preceding terms.

  1. A Data Array: The main and the first necessary thing to load a grid is the data, we will provide a JSON array as data.
  2. A Source: Once the data is ready we will give this array to the source, where source is a JSON property on which we are giving some initial settings. We will look to it when implementing the grid.
  3. A Data Adapter: And once we have created the source element we will give this to another JSON property that is the same as the source with some different settings.
    Now we will start the grid implementation.

Implementing Grid

First we will load a simple grid, then we will implement the nested gird. Sounds good?

The simplest implementation to load a JQWidget JQX grid is as follows.

  1. $(“#jqxgrid”).jqxGrid(
  2. {
  3.     width: 850,
  4.     height: 365,
  5.     sortable: true,
  6.     source: source,
  7.     //rowdetails: true,  
  8.     rowsheight: 35,
  9.     //initrowdetails: initrowdetails,  
  10.     rowdetailstemplate: {
  11.         rowdetails: “<div id=’grid’ style=’margin: 10px;’></div>”,
  12.         rowdetailsheight: 220,
  13.         rowdetailshidden: true
  14.     },
  15.     ready: function() {
  16.         $(“#jqxgrid”).jqxGrid(‘showrowdetails’, 1);
  17.     },
  18.     columns: [{
  19.         text: ‘User Id’,
  20.         datafield: ‘UserId’,
  21.         hidden: true
  22.     }, {
  23.         text: ‘My Transactions’,
  24.         datafield: ‘TransactionName’
  25.     }]
  26. });

In the preceding code I knowingly commented two properties. Have you noticed it?

  1. //rowdetails: true,
  2. //initrowdetails: initrowdetails,

The preceding specified properties are the properties we need to assign for the nested grid.

We can set the data and assign it to the source as follows for the simple grid.

  1. var tdData = $.parseJSON(‘[{“UserId”: “5”,”TransactionName”: “TRAN-Chennai”},{“UserId”: “6”,”TransactionName”: “TRAN-Kerala”}]’);
  2. var source =
  3. {
  4.     datafields: [{
  5.         name: ‘UserId’,
  6.         type: ‘int’
  7.     }, {
  8.         name: ‘TransactionName’,
  9.         type: ‘string’
  10.     }],
  11.     id: ‘UserId’,
  12.     datatype: “array”,
  13.     async: false,
  14.     localdata: tdData
  15. };
  16. var ordersDataAdapter = new $.jqx.dataAdapter(ordersSource, {
  17.     autoBind: true
  18. });
  19. orders = ordersDataAdapter.records;

Now if you run the page you will get output as follows.

If your grid is not populated, please go to your browser console and check whether the required files are loaded correctly.

Now it is time to implement the nested grid.

Implementing the Nested Grid

So our new settings for the parent grid must be as follows.

  1. $(“#jqxgrid”).jqxGrid(
  2. {
  3.     width: 850,
  4.     height: 365,
  5.     sortable: true,
  6.     source: source,
  7.     rowdetails: true,
  8.     rowsheight: 35,
  9.     initrowdetails: initrowdetails,
  10.     rowdetailstemplate: {
  11.         rowdetails: “<div id=’grid’ style=’margin: 10px;’></div>”,
  12.         rowdetailsheight: 220,
  13.         rowdetailshidden: true
  14.     },
  15.     ready: function() {
  16.         $(“#jqxgrid”).jqxGrid(‘showrowdetails’, 1);
  17.     },
  18.     columns: [{
  19.         text: ‘User Id’,
  20.         datafield: ‘UserId’,
  21.         hidden: true
  22.     }, {
  23.         text: ‘My Transactions’,
  24.         datafield: ‘TransactionName’
  25.     }]
  26. });

And now we will create the function initrowdetails, you can see the code here.

  1. var nestedGrids = new Array();
  2. // create nested grid.  
  3. var initrowdetails = function(index, parentElement, gridElement, record)
  4. {
  5.     var id = record.uid.toString();
  6.     var grid = $($(parentElement).children()[0]);
  7.     nestedGrids[index] = grid;
  8.     var filtergroup = new $.jqx.filter();
  9.     var filter_or_operator = 1;
  10.     var filtervalue = id;
  11.     var filtercondition = ‘equal’;
  12.     var filter = filtergroup.createfilter(‘stringfilter’, filtervalue, filtercondition);
  13.     // fill the orders depending on the id.  
  14.     var ordersbyid = [];
  15.     for (var m = 0; m < orders.length; m++) {
  16.         var result = filter.evaluate(orders[m][“UserId”]);
  17.         if (result)
  18.         ordersbyid.push(orders[m]);
  19.     }
  20.     var secondaryDataFields = $.parseJSON(‘[{ “name”: “UserId”, “type”: “int” },{ “name”: “TransactionName”, “type”: “string” },{ “name”: “tranDate”, “type”: “string” },{ “name”: “tranUser”, “type”: “string” }]’);
  21.     var orderssource = {
  22.         datafields: secondaryDataFields,
  23.         id: ‘UserId’,
  24.         localdata: ordersbyid
  25.     }
  26.     var nestedGridAdapter = new $.jqx.dataAdapter(orderssource);
  27.     var secondaryGridColumn = $.parseJSON(‘[{ “text”: “User Id”, “datafield”:”UserId” , “hidden”:”true”},{ “text”: “Transaction Name”, “datafield”:”TransactionName” },{ “text”: “Transaction Date”, “datafield”:”tranDate” },{ “text”: “User”, “datafield”:”tranUser” }]’);
  28.     if (grid != null) {
  29.         grid.jqxGrid({
  30.             source: nestedGridAdapter,
  31.             width: 780,
  32.             height: 200,
  33.             columns: secondaryGridColumn
  34.         });
  35.     }
  36. }

You can see that the settings provided are the same as we provided for the parent grid and please note that the “id” in both of the sources are the same. We provide “UserId” as the key.

Complete Code

The following is the complete code.

  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta name=“viewport” content=“width=device-width” />
  5.         <link href=“jqwidgets/styles/jqx.base.css” rel=“stylesheet” />
  6.         <script type=“text/javascript” src=“jqwidgets/jquery-1.9.1.js”></script>
  7.         <script type=“text/javascript”>
  8.     $(document).ready(function() {
  9.     var tdData = $.parseJSON(‘[{“UserId”: “5”,”TransactionName”: “TRAN-Chennai”},{“UserId”: “6”,”TransactionName”: “TRAN-Kerala”}]’);
  10.     var source =
  11.     {
  12.         datafields: [{
  13.             name: ‘UserId’,
  14.             type: ‘int’
  15.         }, {
  16.             name: ‘TransactionName’,
  17.             type: ‘string’
  18.         }],
  19.         id: ‘UserId’,
  20.         datatype: “array”,
  21.         async: false,
  22.         localdata: tdData
  23.     };
  24.     var orderdetailsurl = $.parseJSON(‘[{“UserId”: “5”,”TransactionName”: “Adyar”,”tranDate”: “04/22/2015 1:35:52″,”tranUser”: “skannan”},{“UserId”: “5”,”TransactionName”: “Guindy”,”tranDate”: “04/22/2015 1:35:52″,”tranUser”: “skannan”},{“UserId”: “6”,”TransactionName”: “Trichur”,”tranDate”: “04/21/2015 1:46:50″,”tranUser”: “bmahadevan”}]’);
  25.     var secondGridData = $.parseJSON(‘[{ “name”: “UserId”, “type”: “int” },{ “name”: “TransactionName”, “type”: “string” },{ “name”: “tranDate”, “type”: “string” },{ “name”: “tranUser”, “type”: “string” }]’);
  26.     var ordersSource =
  27.     {
  28.         datafields: secondGridData,
  29.         datatype: “array”,
  30.         localdata: orderdetailsurl,
  31.         async: false
  32.     };
  33.     var ordersDataAdapter = new $.jqx.dataAdapter(ordersSource, {
  34.         autoBind: true
  35.     });
  36.     orders = ordersDataAdapter.records;
  37.     var nestedGrids = new Array();
  38.     // create nested grid.
  39.     var initrowdetails = function(index, parentElement, gridElement, record) {
  40.         var id = record.uid.toString();
  41.         var grid = $($(parentElement).children()[0]);
  42.         nestedGrids[index] = grid;
  43.         var filtergroup = new $.jqx.filter();
  44.         var filter_or_operator = 1;
  45.         var filtervalue = id;
  46.         var filtercondition = ‘equal’;
  47.         var filter = filtergroup.createfilter(‘stringfilter’, filtervalue, filtercondition);
  48.         // fill the orders depending on the id.
  49.         var ordersbyid = [];
  50.         for (var m = 0; m < orders.length; m++) {
  51.             var result = filter.evaluate(orders[m][“UserId”]);
  52.             if (result)
  53.             ordersbyid.push(orders[m]);
  54.         }
  55.         var secondaryDataFields = $.parseJSON(‘[{ “name”: “UserId”, “type”: “int” },{ “name”: “TransactionName”, “type”: “string” },{ “name”: “tranDate”, “type”: “string” },{ “name”: “tranUser”, “type”: “string” }]’);
  56.         var orderssource = {
  57.             datafields: secondaryDataFields,
  58.             id: ‘UserId’,
  59.             localdata: ordersbyid
  60.         }
  61.         var nestedGridAdapter = new $.jqx.dataAdapter(orderssource);
  62.         var secondaryGridColumn = $.parseJSON(‘[{ “text”: “User Id”, “datafield”:”UserId” , “hidden”:”true”},{ “text”: “Transaction Name”, “datafield”:”TransactionName” },{ “text”: “Transaction Date”, “datafield”:”tranDate” },{ “text”: “User”, “datafield”:”tranUser” }]’);
  63.         if (grid != null) {
  64.             grid.jqxGrid({
  65.                 source: nestedGridAdapter,
  66.                 width: 780,
  67.                 height: 200,
  68.                 columns: secondaryGridColumn
  69.             });
  70.         }
  71.     }
  72.     // creage jqxgrid
  73.     $(“#jqxgrid”).jqxGrid(
  74.     {
  75.         width: 850,
  76.         height: 365,
  77.         sortable: true,
  78.         source: source,
  79.         rowdetails: true,
  80.         rowsheight: 35,
  81.         initrowdetails: initrowdetails,
  82.         rowdetailstemplate: {
  83.             rowdetails: “
  84.             <div id=‘grid’ style=‘margin: 10px;’></div>“,
  85.             rowdetailsheight: 220,
  86.             rowdetailshidden: true
  87.         },
  88.         ready: function() {
  89.             $(“#jqxgrid”).jqxGrid(‘showrowdetails’, 1);
  90.         },
  91.         columns: [{
  92.             text: ‘User Id’,
  93.             datafield: ‘UserId’,
  94.             hidden: true
  95.         }, {
  96.             text: ‘My Transactions’,
  97.             datafield: ‘TransactionName’
  98.         }]
  99.     });
  100. });
  101. < /script>
  102.         <title>JQWidget JQX Nested Grip Implementation @ sibeeshpassion </title>
  103.     </head>
  104.     <body class=‘default’>
  105. JQWidget JQX Nested Grip Implementation @ sibeeshpassion
  106.         <br />
  107.         <div id=“jqxgrid”></div>
  108.         <script type=“text/javascript” src=“jqwidgets/jqxcore.js”></script>
  109.         <script type=“text/javascript” src=“jqwidgets/jqxdata.js”></script>
  110.         <script type=“text/javascript” src=“jqwidgets/jqxbuttons.js”></script>
  111.         <script type=“text/javascript” src=“jqwidgets/jqxscrollbar.js”></script>
  112.         <script type=“text/javascript” src=“jqwidgets/jqxmenu.js”></script>
  113.         <script type=“text/javascript” src=“jqwidgets/jqxgrid.js”></script>
  114.         <script type=“text/javascript” src=“jqwidgets/jqxgrid.selection.js”></script>
  115.         <script type=“text/javascript” src=“jqwidgets/jqxgrid.filter.js”></script>
  116.         <script type=“text/javascript” src=“jqwidgets/jqxgrid.sort.js”></script>
  117.     </body>
  118. </html>

Output

Now if things goes well, you will get output as follows.

Conclusion

I hope you liked this article. Please provide your valuable suggestions. It matters a lot. Please download the source code to learn more.

Point of interest

JQX Grid, JQWidget, JQX Nested Grid.

Exit mobile version