Loop through data

Loop through data

jlcorryjlcorry Posts: 3Questions: 0Answers: 0

What is the easiest/best way to loop through the table (v. 1.10)? I need access to the CSS classes, and to the data itself.

Pseudo code:

var array = [];

for each row {
if (!row.hasClass(x)) {
for each cell {
if (!cell.hasClass(x)) {
array.push(cell.value());
}
}
}
}

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Using the rows(), cells() and each() method:

    var array = [];
    
    table.rows().eq(0).each( function (idx) {
      var row = table.row( idx );
    
      if ( ! $(row.node()).hasClass(x) ) {
        table.cells( idx, null ).eq( 0 ).each( function (cellIdx) {
          var cell = table.cell( cellIdx );
    
          if ( ! $(cell.node()).hasClass(x) ) {
             array.push( cell.data() );
          }
        }
      }
    } );
    

    A better way is simply the following if you are just using class logic:

    var array = table.cells( 'tr:not(.x) td:not(.x)' ).data().toArray();
    

    Allan

This discussion has been closed.