How to keep the child grid open in initial rendering
Initially the child grid or details row in grid will be in collapse state. The Grid uses lazy rendering technique to render the child grid or details row and hence they will rendered only after clicking the expand button at the left most column of the grid.
To keep the child grid/ details row open in the initial rendering itself, we need to invoke the expandAll grid`s public function in the dataBound event.
The dataBound event is the suitable event to call the expandAll method at initial rendering. For need to call the expandAll method in the actionComplete event.
The following code example demonstrates how to open the child grid or details row in the initial rendering and also after every grid action.
JS
<div id="Grid"></div>
<script type="text/javascript"> $(function () { $("#Grid").ejGrid({ dataSource: new ej.DataManager(window.employeeView), allowPaging: true, pageSettings: { pageSize: 3 }, columns: [ { field: "EmployeeID", headerText: 'Employee ID', textAlign: ej.TextAlign.Right, width: 75 }, { field: "FirstName", headerText: 'First Name', width: 100 }, { field: "Title", headerText: 'Title', width: 120, visible: true }, { field: "City", headerText: 'City', width: 100 } ], childGrid: { dataSource: window.ordersView, queryString: "EmployeeID", columns: [ { field: "OrderID", isPrimaryKey: true, headerText: "Order ID", width: 80, textAlign: ej.TextAlign.Right }, { field: "CustomerID", headerText: 'Customer ID', width: 80, textAlign: ej.TextAlign.Left }, { field: "Freight", headerText: 'Freight', width: 90, textAlign: ej.TextAlign.Right } ] }, dataBound: "OnDataBound", actionComplete: "OnActionComplete" }); }); </script>
MVC
@(Html.EJ().Grid<EmployeeView>("Grid") .Datasource((IEnumerable<EmployeeView>)ViewBag.employeeView) .AllowPaging() .PageSettings(p => p.PageSize(3)) .Columns(col => { col.Field(p => p.EmployeeID).HeaderText("Employee ID").Add(); col.Field(p => p.FirstName).HeaderText("First Name").TextAlign(TextAlign.Left).Add(); col.Field(p => p.Title).HeaderText("Title").Add(); col.Field(p => p.City).HeaderText("City").Add(); }) .ChildGrid<OrdersView>( child => child.Datasource((IEnumerable<OrdersView>)ViewBag.orderView) .AllowPaging() .Columns(col => { col.Field(p => p.OrderID).HeaderText("Order ID").Width(80).TextAlign(TextAlign.Right).Add(); col.Field(p => p.CustomerID).HeaderText("Customer ID").Width(80).Add(); col.Field(p => p.Freight).HeaderText("Freight").Width(90).TextAlign(TextAlign.Right).Add(); }) ) .ClientSideEvents(evt => evt.DataBound("OnDataBound") .ActionComplete("OnActionComplete")))
ASP
As the ASP.Net Grid don’t support hierarchical bindings, we have used the details template to depict the usage of the expandAll method.
<script id="GridDetail" type="text/x-jsrender"> <div id="detailGrid{{:EmployeeID}}" class="e-childgrid"></div> </script> <ej:Grid ID="Grid" runat="server" AllowPaging="true" DetailsTemplate="#GridDetail"> <PageSettings PageSize="3" /> <Columns> <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="75"/> <ej:Column Field="FirstName" HeaderText="First Name" Width="100"/> <ej:Column Field="Title" HeaderText="Last Name" Width="120"/> <ej:Column Field="City" HeaderText="City" Width="100"/> </Columns> <ClientSideEvents DetailsDataBound="detailGridData" DataBound="OnDataBound" ActionComplete="OnActionComplete" /> </ej:Grid>
function detailGridData(e) { var filteredData = e.data["EmployeeID"]; var data = ej.DataManager(window.ordersView).executeLocal(ej.Query().where("EmployeeID", "equal", parseInt(filteredData))); e.detailsElement.find(".e-childgrid").ejGrid({ dataSource: data, columns: [ { field: "OrderID", isPrimaryKey: true, headerText: "Order ID", width: 80, textAlign: ej.TextAlign.Right }, { field: "CustomerID", headerText: 'Customer ID', width: 80 }, { field: "Freight", headerText: 'Freight', width: 90, textAlign: ej.TextAlign.Right } ] }); e.detailsElement.css("display", ""); }
The expandAll method will be invoked in the dataBound and the actionComplete event as follows.
function OnDataBound(args) { this.expandAll(); } function OnActionComplete(args) { if (args.requestType == "paging") this.expandAll(); }
The below screenshot is displayed as the result of the above code examples.
Open child grid/details rows initially using expandAll method.