Assign TD background color based in a column value

Assign TD background color based in a column value

henpathenpat Posts: 4Questions: 2Answers: 0

Hi all
I have tow columns
"Color_applied"(visible) - "colorHEX" (hide)

I need to fill all "Color_applied" TD with a background-color = "value of the colorHEX column"

Any help is welcome
Cheers

This question has an accepted answers - jump to answer

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29
    Answer ✓

    Yes, it can be done, but it's not native to jqDataTables per se and it involves adding more json data than is rendered. I'm sure there are other ways to do this, but this is how I've done it.

    You don't need to hide your colorHEX column, just don't need to make it a column at all. You pass it in the json, but not as a table column.

    When you define your columns

    , "columns": [
    "data":"Color_applied"
    ...
    ]
    

    When you send your Json

    "data": [
    {"Color_applied","..Contents of the cell ...",
    "colorHex": " ... the color value ..."
    ...
    } ... {...}
    ]
    

    The trick is using a rowCallback. something like this

    , "rowCallback": function(row, data, index) {
    var cellColor = data["colorHex"]; // Assuming in ff00ff format with a leading pound sign
    $("td:eq(0), row").css("background-color",cellColor); // Assuming The first cell (0) is the Color_Applied column
    }
    
  • henpathenpat Posts: 4Questions: 2Answers: 0

    Thanks!!! this works, rowCallback was the trick!

    Only change I did:

    $("td:eq(0)", row)

This discussion has been closed.