)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Essential JS2

Syncfusion jQuery based widgets are no longer in active development. Switch to our pure JavaScript based next generation Essential JS 2 library.

Syncfusion Feedback

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

Platform: jQuery |
Control: ejGrid |
Published Date: January 17, 2018 |
Last Revised Date: February 25, 2020
Tags: grid, childgrid

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

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon