简体   繁体   English

选中取消选中使用数据表时,所有复选框都不适用于所有页面

[英]check uncheck All checkboxes not working for all pages when using datatable

Script: 脚本:

$(document).ready(function() {
    $(function(){
        $("#result").dataTable({ 
            "scrollX" : true, 
                "ordering" : true,
                "order": [[ 1, "asc" ]],
                "info" : true, 
                });
        });  
    $("#all").click(function () { 
        if ($("#all").is(':checked')) {
            $(".checkboxclass").each(function () {
                $(this).prop("checked", true);
            }); 
        } else {
            $(".checkboxclass").each(function () {
                $(this).prop("checked", false);
            });
        }
    }); 
});     

HTML: HTML:

<table class="tg" id="result" style="width: 100%">
    <thead>
        <tr>
            <th class="tg" style="text-align: center"><input type="checkbox" id="all" 
                onclick="toggle(this);" /></th>
        </tr> 
    </thead>
    <tbody>
        <tr th:each="map : ${listID}">
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check"
                th:id="${map['ID']}" th:value="${map['ID']}" /></td>
        </tr>       
    </tbody>
</table>

Checking on one check box selects all but only limited to current page. 选中一个复选框将选择全部但仅限于当前页面。 It is not working for rest of the pages in pagination 它不适用于其他页面的分页

Can anyone help on this issue. 谁能帮助解决这个问题。 Thanks. 谢谢。

Quite likely, you have duplicate IDs; 很可能,您有重复的ID; if you have more than one element with the same ID, for example id="all" , that gives rise to invalid HTML and you cannot predict how different browsers may handle that. 如果您有多个具有相同ID的元素,例如id="all" ,则会产生无效的HTML,并且您无法预测不同的浏览器如何处理它。 Most will usually pick the first, and ignore the rest of the elements. 大多数人通常选择第一个,并忽略其余的元素。

Please change: 请更换:

 <input type="checkbox" id="all" onclick="toggle(this);" />

To: 至:

<input type="checkbox" class="all" ....

So that your code would be: 这样你的代码就是:

$(".all").on('change', function () {
    $(this).closest('table').find('.checkboxclass').prop('checked', this.checked ); 
}); 

UPDATE UPDATE

Thanks for clarifying; 谢谢你澄清; I'll leave the above so that your comments where you've clarified this may remain relevant 我将保留上述内容,以便您澄清这些内容的评论可能仍然有用

Here is how you may resolve that issue: 您可以通过以下方式解决该问题:

    $(function(){
        var table = $("#result").dataTable({ 
            "scrollX" : true, 
                "ordering" : true,
                "order": [[ 1, "asc" ]],
                "info" : true, 
                });
        });  
        $("#all").on('click', function () { 
            var cells = table.api().cells().nodes();
            $( cells ).find('.checkboxclass').prop('checked', this.checked);
        });
    });
    //source: https://www.datatables.net/forums/discussion/22728/datatables-1-10-select-all-checkbox-and-hidden-rows-pages

Please note that $(document).ready(function() { .... }); 请注意 $(document).ready(function() { .... }); and $(function() { .... }); $(function() { .... }); are different ways of writing the same thing -- no need to nest them. 是不同的写同样的方式 - 不需要嵌套它们。

This worked for me. 这对我有用。

if ($("#all").is(':checked')) { 
  $(".checkboxclass", table.fnGetNodes()).each(function () { 
  $(this).prop("checked", true);
  });     
else {
  $(".checkboxclass", table.fnGetNodes()).each(function () {
  $(this).prop("checked", false); 
  })
  }

you have declared on click toggle function but it is not used. 您已在单击切换功能上声明但未使用它。 please find the code snippet it helpful to sort out ur issue. 请找到有助于解决您的问题的代码段。

<table class="tg" id="result" style="width: 100%">
    <thead>
        <tr>
            <th class="tg" style="text-align: center">
                <input type="checkbox" id="all" />
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
        </tr>
    </tbody>
</table>

 $("#all").click(function () {
    if (this.checked) {
        $('.checkboxclass').each(function () {
            this.checked = true;
        })
    }
    else {
        $('.checkboxclass').each(function () {
            this.checked = false;
        })
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM