使用Jquery从DataTable的所有页面中选择所有内容 [英] Select all contents from all pages from a DataTable using Jquery
问题描述
我正在使用其中有多个页面的DataTable.我使用全选(基于jQuery)功能,可以很好地选择当前页面中正在查看的所有记录.但是问题是
I am using a DataTable that has multiple pages in it. I use Select All (jQuery based) functionality which works well to select all records in the current page being viewed. But the problem is
我无法选择表格中不同页面上的所有记录
I am not able to select all records across different pages in a table
如果我在一页中选择记录并移至下一页,则会丢失在第一页中选择的记录.
If I select record in one page and move to next page , records selected in first page are lost.
选择所有脚本
$(document).ready(function() {
$('#checkall').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() {
this.checked = true;
});
}else{
$('.checkbox1').each(function() {
this.checked = false;
});
}
});
});
有人可以帮忙吗?
谢谢
推荐答案
在DOM上使用jQuery,您只会看到可见的行.您将需要访问表的dataTables内部版本,即其缓存".这是一个"checkall"函数,它遍历所有行,更改了.checkbox1
类的复选框的选中状态:
By using jQuery on the DOM you only reach visible rows. You will need to access dataTables internal version of the table, i.e its "cache". Here is a "checkall" function iterating over all the rows, changing the checked state for a checkbox with the class .checkbox1
:
$('#checkall').click(function(event) { //on click
var checked = this.checked;
table.column(0).nodes().to$().each(function(index) {
if (checked) {
$(this).find('.checkbox1').prop('checked', 'checked');
} else {
$(this).find('.checkbox1').removeProp('checked');
}
});
table.draw();
});
演示-> http://jsfiddle.net/05xnxzbd/
demo -> http://jsfiddle.net/05xnxzbd/
以上内容实际上可以通过几种不同的方式来完成.这是最简单的方法,在第一列中有一个我们知道的复选框.使用 to$()
让我们使用jQuery在内容上.
The above can in fact be done in several different ways. This is the most simple approach, with a checkbox we know exists on the first column. Using to$()
let us work with jQuery on the content right away.
您也可以遍历table.rows().data().each(function(..
,并定位包含不同复选框的多个列,等等.
You could iterate over table.rows().data().each(function(..
as well and target multiple columns holding different checkboxes and so on.
这篇关于使用Jquery从DataTable的所有页面中选择所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!