29

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?

6
  • what do you mean? do you wish to display all rows? if so, then you can try this: var table = $('#example').DataTable({ "paging":false }); Commented Mar 16, 2015 at 13:34
  • Thanks for answere,no, I don't want to display all rows, I want to dynamically loop all rows in code, even rows which are paginated Commented Mar 16, 2015 at 13:46
  • maybe you can just used the data.. I mean your source of data that you fill in the dataTable.. I'm not sure.. Commented Mar 16, 2015 at 13:53
  • yes, good idea, but it happens that init dataTable data has been changed by user ( I have a system for allowing user to edit , add, and delete rows without redraw all dataTable). And i wana know things like for exemple what row is the 27th displayed Commented Mar 16, 2015 at 14:15
  • Unfortunately this way may be out of date and does't work with new version (it launchs an error) What is the error that it throws? Commented Mar 16, 2015 at 19:29

4 Answers 4

45

I finally found:

 var data = table.rows().data();
 data.each(function (value, index) {
     console.log(`For index ${index}, data value is ${value}`);
 });
Sign up to request clarification or add additional context in comments.

4 Comments

Warning: This only iterates over the data itself that was passed in to dataTables - so any HTML you give it will be a string in this method.
How to get specific column values only?
I only get the first page data. How to get the next page ?
You can do it even easier: var data = table.data();
13

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());
});

1 Comment

By this we will get rows of current pager. How can i get all pagers data.
7

If you are using the legacy DataTables then you can get all the rows even the paginated ones, as shown below...

table.fnGetNodes(); // table is the datatables object.

So we can loop through the rows by using .each() method provided by jQuery.

jQuery(table.fnGetNodes()).each(function () {
// You can use `jQuery(this).` to access each row, and process it further.            
});

Comments

2

for example, this data has three fields UserID, UserName and isActive and we want to show only active users The following code will return all the rows.

var data = $('#myDataTable').DataTable().rows().data();

We will print only active users

data.each(function (value, index) {
  if (value.isActive)
  {
     console.log(value.UserID);
     console.log(value.UserName);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.