Mvc Contrib grid with checkbox

ε祈祈猫儿з 提交于 2019-12-01 12:02:52

问题


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>  

回答1:


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');
        }
    });
});



回答2:


The following has worked for me :

column.For(x => Html.CheckBox('chkBox', x.Published)).Named('Published');


来源:https://stackoverflow.com/questions/4273236/mvc-contrib-grid-with-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!