问题
How to remeber checked boxes values with paging. when i checked values on 3 pages. it only storing last page values and other values removing.
i used below with client side processing .
$('.button').click(function () {
var id = "";
var oTable = $("#example").dataTable();
$(".checkboxClass:checked", oTable.fnGetNodes()).each(function () {
});
});
回答1:
Check out jQuery DataTables Checkboxes extension and server-side processing example, where state of checkboxes is preserved.
For example:
var table = $('#example').DataTable({
'processing': true,
'serverSide': true,
'ajax': '/lab/jquery-datatables-checkboxes/ids-arrays.php',
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
We are also working on adding state saving/loading capability very soon which will allow to preserve state of checkboxes between page reloads.
回答2:
could you please use the value in to session-storage rather than var id = "" .
回答3:
It won't work. Because if click next page in pagination it will remove previous page elements in DOM and replace with new elements.
But we can store in array then use it in ajax.
var checkedArray=[];
$(document).on('change','.checkboxClass',function(){
if( $(this).is(':checked') )
{
//if checked add to array
checkedArray[checkedArray.length]=$(this).val();
}else{
//If unchecked remove the value from the array
var index=checkedArray.indexOf($(this).val());
if (index > -1) {
checkedArray.splice(index, 1);
}
}
});
//then you can use it in click event
$('.button').click(function () {
var id = "";
//read checkedArray using for loop
});
Working Fiddle: http://jsbin.com/fewigaxesa/edit?js,console,output
来源:https://stackoverflow.com/questions/41432687/datatable-serverside-process-how-to-remeber-checked-boxes-on-pagination