Is it possible to addClass to render.number

Is it possible to addClass to render.number

fred_17fred_17 Posts: 2Questions: 1Answers: 0

Hi!
I'm trying to add a row with a calculated value and add a class depending on that value, but can't get the addClass to work.

I'm learning so any help is much appreciated!

"data": null,

"render": function ( td, data, type, row ) {

    var sum = data.price - data.prepaid;

    if ( sum = 0 ) {
            $(td).addClass('green');
        } else {
            $(td).addClass('red');
        }
    return $.fn.dataTable.render.number(',', '.', 2, '€ ').display(sum);
    }

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    edited November 2019 Answer ✓

    You would use createdCell to add the class. You will have both the creaetdCell and render options. Something like this:

    "data": null,
    "render": function ( td, data, type, row ) {
     
        var sum = data.price - data.prepaid;
    
        return $.fn.dataTable.render.number(',', '.', 2, '€ ').display(sum);
        },
    "createdCell": function (td, cellData, rowData, row, col) {
    
        var sum = rowData.price - rowData.prepaid;
    
        if ( sum = 0 ) {
                $(td).addClass('green');
            } else {
                $(td).addClass('red');
            }
    
        }
    

    Kevin

  • fred_17fred_17 Posts: 2Questions: 1Answers: 0

    That did the trick!!
    Sanx Kevin!

This discussion has been closed.