23

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?

2
  • what is objective?...getting row data not difficult from API and there are likely examples in the huge download package..post details and code attempts Commented Mar 7, 2012 at 19:01
  • yes getting all row data in an array then I need to use that data to run a query. I have been searching and trying stuff from API for hours but I cant get it so I wanted to give SO a whirl. Commented Mar 7, 2012 at 19:10

4 Answers 4

48

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);
    });
Sign up to request clarification or add additional context in comments.

5 Comments

He doesn't want all rows. He wants a specific column in each node.
I can see how this answers the exact question asked, but I would further wonder what you want to do with those? Reason being, a lot of cell (row and column also) manipulation can be done during the DataTables rendering process using callbacks like fnRowCallback.
but when sorting is applied on any column, your code don't give same data before sorting... which I believe is wrong...
Saved my life! I just wanna add if you are using a checkbox in a column to select the rows you can use this to get the row content $('input[name=checkboxID]:checked', $("#myDataTable").dataTable().fnGetNodes()).closest('tr'); and then use the iteration that Andrew posted. Cheers!
But how if td is hidden. How to get the values?
6

Here's a method using fnGetData()

First get the data from plugin which will be all rows visible or not. Loop over each row data array, and push index=1( second cell) into new array

     oTable = $('#example').dataTable();

      var secondCellArray=[];
      $.each( oTable.fnGetData(), function(i, row){
          secondCellArray.push( row[1]);
    })

     console.log( secondCellArray)

EDit : working demo...look in console after render

http://live.datatables.net/apixiv/edit#javascript,html

Comments

4

jQuery.map combined with fnGetData() makes for a compact solution. The following function will return a one-dimensional array containing all the values from obj_dtable's nth column:

function getDataTableColumn(obj_dtable,n) {
    return $.map(obj_dtable.fnGetData(), function(val) {
        return val[n];
    });
};

Comments

1

You'll want to use the "EQ" selector. It starts at the index of "0", so if you have..

<tr>
  <td>0</td>
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr>

Then by using

 $("td").eq(3); // last one
 $("td").eq(2); //returns "2"

Make sense?

http://api.jquery.com/eq-selector/

1 Comment

that won't work on a paginated table, rows not visible don't exist

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.