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