How to check all the checkboxes in the content on clicking the header checkbox?
Problem
Need to change the state of the content checkboxes on clicking the header checkbox.
Solution
Clicking the header checkbox automatically changes the state of the content checkboxes when the header template is used with template column in the ejGrid. You can set the change client side event to the header checkbox in the Grid create event and it is triggered when checked on the header checkbox. The state of the content checkboxes can be changed in the same header change function.
The actionComplete event is used to maintain its state while performing Grid actions such as paging.
JS
<div id="Grid"> </div> <script type="text/javascript"> $("#Grid").ejGrid({ dataSource: window.gridData, allowPaging: true, create: "onCreate", actionComplete: "onActionComplete", columns: [ { headerText: "Check", template: "<input type='checkbox' class='check' />", headerTemplateID: "#headerTemplate", textAlign: ej.TextAlign.Center, width: 100 }, { field: "OrderID", headerText: "Order ID", width: 75, textAlign: ej.TextAlign.Right }, { field: "CustomerID", headerText: "Customer ID", width: 80 }, { field: "EmployeeID", headerText: "Employee ID", width: 75, textAlign: ej.TextAlign.Right } ] }); </script>
MVC
@(Html.EJ().Grid<HeaderCheck.OrdersView>("FlatGrid") .Datasource((IEnumerable<object>)ViewBag.datasource) .AllowPaging() .Columns(col => { col.HeaderText("Check").Template("<input type='checkbox' class='check' />") .HeaderTemplateID("#headerTemplate").TextAlign(TextAlign.Center).Width(100).Add(); col.Field("OrderID").HeaderText("Order ID").TextAlign(TextAlign.Right).Width(100).Add(); col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add(); col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(100).Add(); }) .ClientSideEvents(eve => eve.ActionComplete("onActionComplete").Create("onCreate")) )
ASP
<ej:Grid ID="FlatGrid" runat="server" AllowPaging="True"> <ClientSideEvents Create="onCreate" ActionComplete="onActionComplete" /> <Columns> <ej:Column HeaderText="Check" Template="<input type='checkbox' class='check' />" HeaderTemplateID="#headerTemplate" TextAlign="Center" Width="100" /> <ej:Column Field="OrderID" HeaderText="Order ID" TextAlign="Right" Width="100" /> <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="100" /> </Columns> </ej:Grid>
The following code example is common for all the three platforms.
JS
// Header Template. <script type="text/x-jsrender" id="headerTemplate"> Checked <input type="checkbox" id="headerCheck" /> </script> <script type="text/javascript"> // Grid creates function. function onCreate(args) { // Sets change event to the header checkbox. $("#headerCheck").ejCheckBox({ "change": "headerChecked" }); } // Header checkbox changes function. function headerChecked(args) { checkAll(args.isChecked); } // Grid actionComplete function. function onActionComplete(args) { if (args.requestType == "paging") // Maintain the checked state while paging checkAll($("#headerCheck").ejCheckBox("model.checked")); } function checkAll(flag) { $(".check").prop("checked", flag); // Check or uncheck all content check boxes } </script>
The following screenshots display the header before being checked, after being checked and paging.
Figure 1: Before the change of header checkbox
Figure 2: After the change of header check box
Figure 3: After paging