Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 521
R learner Avatar asked Nov 28 '25 11:11

R learner


1 Answers

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.

like image 70
Stéphane Laurent Avatar answered Nov 30 '25 01:11

Stéphane Laurent