Articles in this section
Category / Section

How to fix state of the content checkboxes error in MVC Grid?

1 min read

Problem

Need to change the state of the content checkboxes on clicking the header checkbox .

Solution

Using ASP.NET MVC Grid, on 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

Conclusion

I hope you enjoyed learning how to fix the state of content checkboxes error in MVC Grid.

You can refer to ASP.NET MVC 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 ASP.NET MVC 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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