2010-11-25 54 views
1

我正在寻找如何在支持选中或未选中的标题上添加复选框的方式,我的girdview的所有复选框列。带复选框的Mvc Contrib网格

<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> 

回答

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

然后你可以使用jQuery来处理头复选框的变化情况,并检查/取消所有其他:

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

DoNotEncode()已过时。改用Encode(false) – Eldar 2010-11-30 10:47:12

5

以下为我工作:

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