如何在不同页面上实现“检查所有”功能,仅检查jquery数据表中的当前页面数据?

ish*_*han 3 jquery datatables

我有一个表,其中包含大量记录,借助数据表进行分页。第一个标题列是全选复选框,每行都有自己的复选框。

我希望在我的表中具有以下功能:

1) 用户可以浏览表格并随机选择/取消选择复选框。

2) 单击标题中的“全选”复选框应仅选中/取消选中所有当前可见的记录。

3)“全选”按钮应为数据表的不同页面保持状态(选中/未选中)。例如,如果用户单击第 1 页上的“全选”并导航到下一页,则应取消选择“全选”复选框,再次单击该复选框将仅检查此页面中的所有行,而之前选择的复选框不受影响。

到目前为止,我有以下代码来处理检查选择:

$('#selectAllCheck').click(function(e) {

    var chk = $(this).prop('checked');
    var currentRows = $('#myTable tbody tr');

    $.each(currentRows, function(){
        $(this).find(':checkbox[name=statusCheckbox]').each(function(){
            $(this).prop('checked', chk);
        });
    });
  });
Run Code Online (Sandbox Code Playgroud)

我知道这个_('tr', {"filter":"applied"});函数,但它只是将所有行返回给我。我不知道为什么。

我已经使用上面的代码实现了(1)和(2),并且工作正常。唯一的问题是“全选”功能在不同页面上的行为。我查看了 datatables.net 但找不到与此相关的任何内容。

ish*_*han 5

这就是我最终根据我的要求实现的。虽然不完美,但这段代码很好地实现了数据表中的“全选”列。

$(document).ready(function() {
oTable = $('#mytable').dataTable({
    "bJQueryUI" : true,
    "sPaginationType" : "full_numbers",
    "fnDrawCallback": function( settings ) {
            //managing the "Select all" checkbox
            // everytime the table is drawn, it checks if all the 
            //checkboxes are checked and if they are, then the select all
            // checkbox in the table header is selected
            var allChecked = true;
            $('#mytable tbody tr').each(function() {
                $(this).find(':checkbox[name=statusCheckbox]').each(function(){
                    if (!$(this).is(':checked')) {
                        allChecked = false;
                        }
                    });
                });
            $('#selectAllCheck').prop('checked', allChecked);
        },
        });

// This is to stop datatable to sort the column on checking the checkbox
$('thead').click(function(e){
    if (e.target.type == 'checkbox') {
        e.stopPropogation();
    }
});

// Click handler for select all checkbox that checks the rows on current view only
$('#selectAllCheck').click(function(e) {
    var chk = $(this).prop('checked');
    var currentRows = $('#mytable tbody tr');
    $.each(currentRows, function(){
        $(this).find(':checkbox[name=statusCheckbox]').each(function(){
            $(this).prop('checked', chk);
        });
    });
  });
Run Code Online (Sandbox Code Playgroud)

});