Alex
Alex

Reputation: 13

R Shiny DataTable selected row color on specific table

I am trying to apply different color on row selection in Data tables as mentioned in this post: R Shiny DataTable selected row color. As it seems in the in the example below, this applies to all data tables within the app.

library(shiny)
library(DT)

ui <- fluidPage(
  tags$style(HTML('table.dataTable tr.selected td, table.dataTable td.selected {background-color: black !important;}')),
  title = 'Select Table Rows',    
  fluidRow(
    column(6, DT::dataTableOutput('Table1')),
    column(6, DT::dataTableOutput('Table2'))
  )  
)

server <- function(input, output, session) {
  output$Table1 = DT::renderDataTable(cars, server = FALSE)
  mtcars2 = head(mtcars[, 1:8],10)
  output$Table2 = DT::renderDataTable(mtcars2, server = TRUE)
}

shinyApp(ui, server)

Is there a way to explicity specify which table will be affected by this?

Upvotes: 1

Views: 1295

Answers (1)

Shree
Shree

Reputation: 11140

Just need to add #TableID at the beggining of your style statement. Below I am applying the new highlight style only to Table1 -

library(shiny)
library(DT)

ui <- fluidPage(
  tags$style(HTML('#Table1 table.dataTable tr.selected td, table.dataTable td.selected {background-color: black !important;}')),
  title = 'Select Table Rows',    
  fluidRow(
    column(6, DT::dataTableOutput('Table1')),
    column(6, DT::dataTableOutput('Table2'))
  )  
)

server <- function(input, output, session) {
  output$Table1 = DT::renderDataTable(cars, server = FALSE)
  mtcars2 = head(mtcars[, 1:8],10)
  output$Table2 = DT::renderDataTable(mtcars2, server = TRUE)
}

shinyApp(ui, server)

Upvotes: 2

Related Questions