cell.node() undefined for some rows in rowCallback

cell.node() undefined for some rows in rowCallback

kharteveldkharteveld Posts: 2Questions: 1Answers: 0

Hi guys,

I use the following function as a rowCallback to make negative numbers red:

function negativeRed( row, data, index ) {
this.api().cells(index, '.negative-red').every( function() {
console.log(this, this.data(), this.node());
if (this.data() < 0) {
$(this.node()).addClass('text-danger');
} else {
$(this.node()).removeClass('text-danger');
}
});
}

This works fine in small tables, but in larger tables (> 50 rows), this.node() is undefined for the first rows, so it does nothing. Is there anything I can do to solve this.

Thanks!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,331Questions: 26Answers: 4,774
    edited September 2023 Answer ✓

    The problem is likely due to the index you are using for the cells() API. The third parameter of the rowCallback is this:

    Row number in the current page of displayed rows.

    However I think you are trying to use is a row-selector of an integer which is expected to be the row index. The fifth parameter of rowCallback is the row index:

    DataTables' internal index for the row - see row().index()

    Try using the fifth parameter.

    You might be able to do something like this as a jQuery cell-selector:

    this.api().cells( $('.negative-red', row) ).every( function() {
    

    Kevin

  • kharteveldkharteveld Posts: 2Questions: 1Answers: 0

    You are absolutely right, worked like a charm. Thank you so much!

Sign In or Register to comment.