How to bind the Child JavaScript Grid datasource through ajax call?
In JavaScript Grid, you can bind the childGrid data source dynamically by using the following two ways.
- By using the ajax call in Load event of the child Grid.
- By using the detailsExpand client side event of the Grid.
By using ajax call in the Load event of the child grid:
You can bind the child Grid datasource through the ajax call by using load event of the child grid.
Grid Rendering Code:
JS
$("#Grid").ejGrid({ dataSource: data, childGrid: { dataSource:window.ordersView, childGrid: { dataSource: dataManger, queryString: "EmployeeID", allowPaging: true, columns: [ { field: "OrderID", headerText: 'Order ID', width: 75 }, { field: "ShipCity", headerText: 'Ship City', width: 100 }, { field: "Freight", headerText: 'Freight', width: 120 }, { field: "ShipName", headerText: 'Ship Name', width: 100 } ], //Enables the load event load: "loadEvent", //Enables the detailsExpand event detailsExpand: "detailsExp", } });
MVC
@(Html.EJ().Grid<employeeview> ("HierarchyGrid") .Datasource((IEnumerable<object>)ViewBag.datasource) .ChildGrid(child => { child.Datasource("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/") .QueryString("EmployeeID") .AllowPaging() .Columns(col => { col.Field("OrderID").HeaderText("Order ID").Width(75).Add(); col.Field("EmployeeID").HeaderText("Employee ID").Width(75).Add(); col.Field("Freight").HeaderText("Freight").Width(75).Format("{0:C}").Add(); col.Field("ShipName").HeaderText("Ship Name").Width(110).Add(); }) //For enable load event and detailsExpand event for child grid .ClientSideEvents(eve => { eve.Load(“loadEvent”).DetailsExpand(“detailsExp”); } ) }) )
loadEvent code:
JS
<script type="text/javascript"> function loadEvent(args) { var proxy = this; //Here we have passed the new query because while passing data source externally we need to pass the new query this.model.query= new ej.Query(); $.ajax({ url: "http://mvc.syncfusion.com /Services/Northwnd.svc/Orders/ ?$skip=12&$top=12", dataType: "json", success: function (e) { //Here we have passed the retrieved json data to the dataSource method of the child grid. proxy is duplicate of child grid proxy.dataSource(e.d); } }); } </script>
By using the detailsExpand client side event of the Grid:
Child Grid is rendered once the parent Grid gets expanded. You can dynamically change/bind the data source to the child Grid after expanding the parent Grid by using the detailsExpand client side event of the Grid.
Refer to the following code example.
detailsExp Script code:
JS
<script type="text/javascript"> function detailsExp(args) { this.option("model.childGrid.dataSource", window.ordersView); } </script>
Here, the data source is bound to the child Grid through detailsExpand client side event of the Grid.
Result:
Figure 1:Binding dataSource dynamically to child grid
.
Conclusion
I hope you enjoyed learning about how to bind the Child JavaScript Grid data source through ajax call.
You can refer to our JavaScript Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!