2010-11-25 57 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');