我知道这可能看起来很原始,但我已经尝试实现了一整天,可能是因为我无法完全理解如何使用API,我使用的是DataTables 1.10.0,我有一个带有分页功能的表,每行中都有一个复选框,我需要有一个"检查所有按钮",可以检查所有页面中的所有复选框,问题是它只检查当前页面中的复选框,而不检查其他页面,这本应该很容易,但我搞不明白!我发现的答案使用了"fnGetNodes",这似乎是不推荐使用的,1.10版没有使用
编辑:这是我的标记
<table class="table table-striped table-bordered table-hover" id="numbers_table">
<thead>
<tr>
<th><input type="checkbox" id="checkall" title="Select all" onClick="toggle(this)"/></th>
<th>Number</th>
<th>Company</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
<% _.each(array, function (value) { %>
<tr>
<td><input type='checkbox' name='numbers[]' value='<%=value.id%>'/></td>
<td><%= value.number %></td>
<td><%= value.company %></td>
<td><%= value.tags %></td>
</tr>
<% }) %>
</tbody>
</table>
<button type="button" class="btn btn-primary" id="checkall2">SELECT ALL</button>
<script>
$(document).ready(function() {
$('#numbers_table').dataTable({
//"bPaginate": false,
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0 ] }
]
});
$("#checkall2").click(function() { // a button with checkall2 as its id
$(':checkbox').prop('checked', true); // all checkboxes, you can narrow with a better selector
});
});
</script>
这应该适用于
var table = $('#numbers_table').DataTable();
$('#checkall').click(function () {
$(':checkbox', table.rows().nodes()).prop('checked', this.checked);
});
试试这个代码:
var table = $('#numbers_table').DataTable();
var cells = table
.cells( ":checkbox" )
.nodes();
$( cells ).prop('checked', true);
来源。
如果不起作用,请尝试使用api((关键字:
$('#YourCheckBoxId').on('click', function () {
var rows = YourDatatableVariable.api().rows({ 'search': 'applied' }).nodes();
$('input[type="checkbox"]', rows).prop('checked', this.checked);
});
这真的没有那么难,但如果没有看到您的标记,我只能提供一个通用的示例-
$('input[value="Check All"]').click(function() { // a button with Check All as its value
$(':checkbox').prop('checked', true); // all checkboxes, you can narrow with a better selector
});