Question

I'm looking for the way how to add a checkbox on the header that support checked or unchecked all my checkbox column of my girdview.

    <table class="grid">
    <th><input type="checkbox" name="chkall"/></th>
    <th>Name</th>
     <tr>
         <td>       
            <input type="checkbox" id="chkItem_1"/>
         </td>
         <td>
             Category 1
         </td> 
     </tr>
     <tr>
         <td>       
            <input type="checkbox" id="chkItem_2"/>
         </td>
          <td>
             Category 2
         </td>
     </tr>
</table>  
Was it helpful?

Solution

column.For(x => Html.CheckBox("mycheckbox", new { @class = "foo" }))
    .DoNotEncode()
    .Header("<th><input type=\"checkbox\" id="chkHeader" /></th>");

And then you could use jquery to handle the change event of the header checkbox and check/uncheck all the others:

$(function() {
    $('#chkHeader').change(function() {
        if ($(this).is(':checked')) {
            $('.foo').attr('checked', 'checked');
        } else {
            $('.foo').removeAttr('checked');
        }
    });
});

OTHER TIPS

The following has worked for me :

column.For(x => Html.CheckBox('chkBox', x.Published)).Named('Published');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top