Cell background color based on cell content

Cell background color based on cell content

dmolavidmolavi Posts: 65Questions: 0Answers: 0
edited August 2012 in DataTables 1.9
For a new site I'm writing, I would like to be able to specify the background color of the table cell based on the value within that cell. For example:
value < 6, bgcolor = red
6 < value < 8, bgcolor = green
value > 8, bgcolor = red

Is this possible? The data will be gotten by JSON.

Replies

  • The_CoopThe_Coop Posts: 1Questions: 0Answers: 0
    I'm new to DataTables, so don't know if I'm doing it correctly or not but I had a similar problem where I wanted the background colour of the cell to change depending upon the result of a game, i.e. won, drawn or lost. This is what I came up with, and it works for me. Would have preferred to have passed a css class instead but couldn't work that one out! If anyone can, please let me know.

    The code:

    { "aTargets": [5], "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
    if (sData == 'W') {
    $(nTd).css('background-color', '#DFF2BF').css('color', '#4F8A10').css('font-weight', 'bold');
    } else if (sData == 'D') {
    $(nTd).css('background-color', '#BDE5F8').css('font-weight', 'bold').css('color', '#009');
    } else {
    $(nTd).css('font-weight', 'bold').css('background-color', '#FFBABA').css('color', '#D8000C');
    }
    } }
  • neskenessneskeness Posts: 1Questions: 0Answers: 0
    Hello, similar problem here: I want to make change the font color based on cell contents. I too am very new to DataTables. Can you post more of your code? I'm not sure where/when this code neds to take place.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Take a look at this example: http://datatables.net/release-datatables/examples/advanced_init/row_callback.html
  • joao_oliveirajoao_oliveira Posts: 32Questions: 0Answers: 0
    You managed to solve, I'm having the same problem.
This discussion has been closed.