Articles in this section
Category / Section

How to bind parent grid dataSource's inner array as dataSource for child grid?

2 mins read

This explains about binding parent grid dataSource inner array as dataSource for child grid.

Solution:

We achieved using DetailsExpand event of the parent grid. In detailsExpand event we can get the inner array object corresponding to the parent record and assign that data to the childGrid in the load event of the childgrid.

The following code example demonstrates how to bind parent grid dataSource inner array as dataSource for child grid:

JS:

1. Render the Grid Control.

<div id="Grid"></div>
 
    <script type="text/javascript">
        $("#Grid").ejGrid({
            dataSource: window.Data,
            detailsExpand: "detailsExpand",
            columns: [
                    { field: "EmployeeID", headerText: "Employee ID", textAlign: ej.TextAlign.Right, width : 75, isPrimaryKey : true },
                    { field: "FirstName", headerText: "First Name", width : 100 },
                    { field: "City", width : 100 },
            ],
            childGrid: {
                queryString: "EmployeeID",
                allowPaging: true,
                load : "load",
                columns: [
                  { field: "OrderID", headerText: 'Order ID', textAlign: ej.TextAlign.Right, width: 75 },
                  { field: "ShipCity", headerText: 'ShipCity', width: 100 },
                  { field: "ShipName", width: 100 }
                ],
            }
        });
    </script>

 

MVC:

@(Html.EJ().Grid<object>("Grid")
      .Datasource((IEnumerable<object>)ViewBag.datasource)
      .ClientSideEvents(eve => eve.DetailsExpand("detailsExpand"))
      .Columns(col =>
      {
          col.Field("EmployeeID").HeaderText("Employee ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
          col.Field("FirstName").HeaderText("First Name").Width(100).Add();
          col.Field("City").Width(100).Add();
      })
     .ChildGrid(ordersGrid =>
      {
          ordersGrid
            .QueryString("EmployeeID")
            .AllowPaging()
            .ClientSideEvents(eve => eve.Load("load"))
            .Columns(col =>
             {
                 col.Field("OrderID").HeaderText("OrderID").TextAlign(TextAlign.Right).Width(75).Add();
                 col.Field("ShipCity").HeaderText("ShipCity").Width(100).Add();
                 col.Field("ShipName").Width(100).Add();
             });
       })
 )         .ChildGrid(ordersGrid =>
         {
             ordersGrid
              .QueryString("ID")
              .ClientSideEvents(eve=>eve.Create("create"))
             .Columns(cols =>
             {
                 cols.Field("CustomerID ").Add();
                 cols.Field("CustomerName ").Add();
             });
         })

 

ASP.NET CORE:

<ej-grid id="HierarchyGrid" datasource="ViewBag.datasource" detailsexpand="detailsExpand">
    <e-columns>
        <e-column field="EmployeeID" header-text="Employee ID" is-primary-key="true" text-align="Right" width="75"></e-column>
        <e-column field="FirstName" header-text="First Name" width="100"></e-column>
        <e-column field="City" width="100"></e-column>
    </e-columns>
    <ej-grid query-string="EmployeeID" allow-paging="true" load="load">
        <e-columns>
            <e-column field="OrderID" header-text="OrderID" text-align="Right" width="75"></e-column>
            <e-column field="ShipCity" header-text="ShipCity" width="100"></e-column>
            <e-column field="ShipName" width="100"></e-column>
        </e-columns>
    </ej-grid>
</ej-grid>

 

Angular:

<ej-grid id="Grid" [dataSource]="gridData" (detailsExpand)="detailsexpand($event)" >
    <e-columns>
        <e-column field="EmployeeID" headertext="Employee ID" width="75" textalign="right"></e-column>
        <e-column field="FirstName" headertext="First Name" width="100"></e-column>
        <e-column field="City" width="100"></e-column>
    </e-columns>
</ej-grid>

 

TS File:

@ViewChild('grid') GridModel: EJComponents<any, any>;
 
    export class GridDetailTempComponent {
 
      public gridData: any;
 
      public childData : any;
 
      constructor() {
       
        this.gridData = (window as any).Data;
 
        this.childData = {
            queryString: "EmployeeID",
            allowPaging: true,
            load : function(args) {
                this.model.dataSource = (window as any).childData;
            },
            columns: [
                { field: "OrderID", headerText: 'OrderID', textAlign: ej.TextAlign.Right, width: 75 },
                { field: "ShipCity", headerText: 'ShipCity', width: 100 },
                { field: "ShipName", width: 100 }
            ],  
        }  
    }
 
    detailsexpand(e:any){
        (window as any).childData = e.masterData.Orders;
    } 
}

 

JS:

2. In detail expand event we store inner array object of corresponding parent record to the window.childData and assign that data to the childGrid in the load event of the childgrid.

function detailsExpand(args) {
        window.childData = args.masterData.Orders;
 
    }
 
function load(args) {
        this.model.dataSource = window.childData;
    }
 

 

 

Result:

 

Hierarchy Grid

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied