jQuery DataTables - Access all rows data

2019-04-06 05:37发布

问题:

I'm using jQuery DataTables and want to copy all rows (save in JavaScript array) upon clicking the header checkbox.

I want to find where jQuery DataTables store the HTML for remaining page of rows, so I can navigate through JavaScript then check it there or set property checked to true.

Something like this one.

Other information:

  • I use data from an ajax source(serverside:false), all data is returned.
  • When I click page 1, all the rows remain Checked.

回答1:

SOLUTION

There are many methods that could be used for that purpose. You can use rows().data() to get the data for the selected rows.

Example:

var table = $('#example').DataTable();

var data = table
    .rows()
    .data();

alert( 'The table has ' + data.length + ' records' );

DEMO

See this jsFiddle for code and demonstration.



回答2:

I find the generated element by jQuery DataTables using this code, and I can copy the whole tr element that hides when paging the DataTables.

$('#example').DataTable().rows().iterator('row', function(context, index){
    var node = $(this.row(index).node()); 
    //node.context is element of tr generated by jQuery DataTables.
});


回答3:

use tableObject.rows().data() will return all data of table.