R learner
R learner

Reputation: 101

Add custom class label to datatable row

Is there a possibility to add a costum class label to a data.table row?

I formated my DT row using formatStyle to a yellow background in case a certain condition is fullfilled.

DT::formatStyle(1, target = "row", backgroundColor = DT::styleEqual(trans_age(), c("yellow"))) %>%

However, the color is not shown when I want to print the table using the print function from my web-browser. Therefore, my idea is to add a class label to the specific, and to set the colour for the print via a costum css using @media print.

Kind regards, Silke

Upvotes: 3

Views: 644

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84529

With the createdRow option:

library(DT)

dat <- iris[1:3,]

js <- JS(
  "function( row, data, dataIndex ) {",
  "  if(dataIndex === 2) {",
  "    $(row).addClass('myclass');",
  "  }",
  "}"
)

datatable(
  dat, 
  options = list(
    createdRow = js
  )
)

This add the class myclass to the third row, since indexing starts at 0 in JavaScript.

Upvotes: 4

Related Questions