Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through all rows in DataTables jQuery?

I am using jquery plugin DataTables for building nice table

  var table = $('#example').DataTable({     "data": source }); 

I would like that make an each for all rows in table

Unfortunately this way may be out of date and does't work with new version (it launchs an error)

$(table.fnGetNodes()).each(function () {  }); 

And this way only works only for visibles rows (10 first rows because other rows are paginated)

 table.each( function ( value, index ) {     console.log( 'Data in index: '+index+' is: '+value ); } ); 

Do you known how to loop to all rows please?

like image 744
Pipo Avatar asked Mar 16 '15 13:03

Pipo


People also ask

How can get column data from DataTable in jquery?

var myTable = $("#tblResults"). DataTable(); var resultsArray = myTable. columns(colIndex). data();

How do I find DataTable data?

You can use rows(). data() to get the data for the selected rows.


2 Answers

I finally found:

 var data = table.rows().data();  data.each(function (value, index) {      console.log(`For index ${index}, data value is ${value}`);  }); 
like image 79
Pipo Avatar answered Sep 22 '22 14:09

Pipo


Datatables have an iterator for each row rows().every() with this referring to the context of the current row being iterated.

tableName.rows().every(function(){     console.log(this.data()); }); 
like image 38
Athman Avatar answered Sep 22 '22 14:09

Athman