Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through DataTables table to get all cells content

Tags:

I am using jquery dataTables to generate the paginated table on my site. I need to run a process that grabs all of the data out of a particular column. Something like :

$('.testLink').click(function(){             var cells = new Array();             $('#myTable tr td').each(function(){                 cells.push($(this).html());             });             console.log(cells);         }); 

That example grabs everything but I would need just the information from one column of tds. I guess I could do that by adding a class to all of the tds in that row but I am sure there is a better way. That is a bonus question..

but what I really want to know is how to get this to work with datatables? Because the script hides most of the table to put in pagination this function only grabs the cells that are visible. I played around with fnGetData but I am not getting it. Any ideas?

like image 554
Zac Avatar asked Mar 07 '12 18:03

Zac


1 Answers

To access all the rows, you can do:

var rows = $("#myTable").dataTable().fnGetNodes(); 

In your case, this should work:

   $('.testLink').click(function(){         var cells = [];         var rows = $("#myTable").dataTable().fnGetNodes();         for(var i=0;i<rows.length;i++)         {             // Get HTML of 3rd column (for example)             cells.push($(rows[i]).find("td:eq(2)").html());          }         console.log(cells);     }); 
like image 56
Diego Avatar answered Nov 24 '22 00:11

Diego