Formatting cells based on another column

Formatting cells based on another column

japinjapanjapinjapan Posts: 5Questions: 1Answers: 0
edited May 2019 in Free community support

I'm using:

columnDefs: [
{
targets: 7,
createdCell: function (td, cellData, rowData, row, col) {
if ( cellData <  "D" ) {
$(td).css('color', 'white')
$(td).css('background-color', 'red')
} else {
$(td).css('color', 'white')
$(td).css('background-color', 'blue')
}
}
} ],

to highlight a column red or blue based on values within that column.

Can the code be adapted to highlight a column based on values in a different column?

Thanks

Replies

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Yep, note the third argument is rowData - you can use that in your logic too...

  • japinjapanjapinjapan Posts: 5Questions: 1Answers: 0

    Great, thanks Colin I'll give it a go!

  • japinjapanjapinjapan Posts: 5Questions: 1Answers: 0

    To answer my own question, the following code highlights the 10th and 11th columns, based on the value in the 12th column:

    {
    targets: [10, 11],
    createdCell: function (td, cellData, rowData, row, col) {
    if ( rowData[12] == "T" ) {
    $(td).css('color', 'white')
    $(td).css('background-color', 'blue')
    } else if ( rowData[12] == "C" ){
    $(td).css('color', 'white')
    $(td).css('background-color', 'red')
    } else {
    $(td).css('color', 'black')
    $(td).css('background-color', 'white')
    }
    }
    } ,
    
This discussion has been closed.