DataTable serverside process how to remeber checked boxes on pagination

折月煮酒 提交于 2019-12-08 10:13:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!