Get data for each cell in each visible row when table data in objects

Get data for each cell in each visible row when table data in objects

nmpcnmpc Posts: 1Questions: 1Answers: 0
edited November 2015 in Free community support

I want to iterate over each visible cell in each visible row and process the cell's data, but I can't find a good way of doing it.

I have a table populated with data from objects. Not all properties of the data objects are shown as columns in the table:
http://live.datatables.net/xorumiqi/7/

If I use rows.every() and row.data(), then the object I get back includes properties that are not used in the table, and I have no way of knowing which properties are shown in the table and which are not.

The only way I've found of getting the data I want is to use rows.every() and cells.every() and compare the row index to check whether the current cell is in the row I'm interested in:

  table.rows({search: 'applied'}).every( function ( rowIdx, tableLoop, rowLoop ) {
    // This gets all data in the row's object (including data that's not shown)
    console.log(this.data());
    
    // this is the only way I can find of getting cells from the current row,
    // but this checks all cells in all rows for each row (too slow for large tables)
    cells = this.cells(function (cellIdx, data, node) {
      return (cellIdx.row == rowIdx);
    });
    
    cells.every(function() {
      console.log(this.data());
    });
  });

This isn't feasible for large tables because for every visible row it checks all cells in the table.

What is the best way of iterating over each visible cell in each visible row?

This question has an accepted answers - jump to answer

Answers

  • sbpcsbpc Posts: 1Questions: 0Answers: 1
    Answer ✓

    I have found the best way to iterate over each visible row and process the cell data within that row is to use the node object of the row. Pass the node into jQuery, then find and iterate over all of the "td" elements from the node using jQuery. This will get you all the data from each cell in every row so you can process it.

    Basically you can do something like this:

    table.rows({search:'applied'}).every( function ( rowIdx, tableLoop, rowLoop ) {
    
        var rowNode = this.node();
    
            $(rowNode).find("td:visible").each(function (){
    
                      var cellData = $(this).text();
                      //do something with the cell data.
       
               });
    }); 
    

    This should work with every visible cell in every visible row as long as you have {search:'applied'} to the rows.

This discussion has been closed.