Search code examples
jquerydatatablerow

How can I update the cell index of datatables?


I would like to get the data of the row by row number in my datatable.

$('.table').on( 'draw.dt', function() {
    console.log(table.row(0).data());
    console.log(table.row(1).data());
}

The problem is, that the columns are sorted and that changes the number for the row. So to really get the data of row 0 and 1 I have to write it like this:

console.log(table.row(27).data());
console.log(table.row(3).data());

This would give the correct result, but of course this does not help me, because I need to search by the number of the position (after sorted) in the datatable.


Solution

  • This updates The index of the cell:

      table.on( 'order.dt search.dt', function () {
        table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
          cell._DT_CellIndex.row = i;
        } );
      } ).draw();