1


I'd like to implement a conditional formating. E.g.

library(DT)

datatable(mtcars, options = list(
  fnRowCallback = JS(
    "function ( row, data, index ) {",
    "if ( data[3] > 130 ) {",
        "$(data[3]).css('background-color', '#078DC6');",
      "}",
    "else {",
    "$(data[3]).css('background-color', '#FFDE00');",
      "}",
    "}")))

Here I want to change background of rows in terms of disp column.How to upgrade this? Right now this code does not change backgorund of rows at all.

1

1 Answer 1

1

The css need to be applied to the row, not the data, so you can try something like this:

library(DT)
datatable(mtcars, options = list(
  fnRowCallback = JS(
    "function ( row, data, index ) {",
    "if ( data[2] > 130 ) {",
    "$(row).css('background-color', '#078DC6');",
    "}",
    "else {",
    "$(row).css('background-color', '#FFDE00');",
    "}",
    "}")))

Also, array indexes start at 0 in JS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.