hansi silva
hansi silva

Reputation: 99

DataTable serverside process how to remeber checked boxes on pagination

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 () {


    });
});

Upvotes: 0

Views: 5121

Answers (3)

Sarath Kumar
Sarath Kumar

Reputation: 1146

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

Upvotes: 0

Gyrocode.com
Gyrocode.com

Reputation: 58900

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.

Upvotes: 1

Bhabani P
Bhabani P

Reputation: 1361

could you please use the value in to session-storage rather than var id = "" .

Upvotes: 0

Related Questions