jquery - 数据表 1.10 "Check all"通过 jquery

标签 jquery datatables

我知道这可能看起来很原始,但我一整天都在尝试实现它,也许是因为我无法完全理解如何使用 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);
    });

关于jquery - 数据表 1.10 "Check all"通过 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637586/

相关文章:

javascript - 使用 Javascript/jQuery 动态填充下拉列表

javascript - 无法使用 search() API 的自定义正则表达式实现进行 stateSave

javascript - 使用 jquery 选择列表中的第二个和第三个列表项

javascript - 了解一个 JQuery 复杂链接代码示例

jquery - 数据表。提交后保留所选行

javascript - 数据表 o语言(国际化)默认设置

ruby-on-rails-4 - Rails 4 上的数据表 ruby

jQuery Datatable,通过 UI 调整列宽度

javascript - 从 100% 过渡到自动

javascript - 从多维数组中获取最小-最大数字