在 Shiny 应用程序中使用 bslib 主题更改数据表中选定行的颜色

问题描述 投票:0回答:2

我想将所选行的颜色从蓝色更改为其他颜色。这是可能的,无需使用 bslib 包和主题,但我想使用主题,并且仍然能够更改颜色。

这有效:

library(shiny)
library(DT)
library(bslib)

ui <- fluidPage(
  #theme = bs_theme(),
  tags$style(HTML('table.dataTable tr.selected td, table.dataTable td.selected {background-color: pink !important;}')),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {
  
  output$mytable = DT::renderDataTable(    
    datatable(mtcars)
  ) 
}
runApp(list(ui = ui, server = server))

取消注释主题行会将颜色恢复为蓝色。

我被难住了。有什么想法吗??

r shiny dt bslib
2个回答
2
投票

试试这个:

ui <- fluidPage(
  theme = bs_theme(),
  tags$style(HTML("
      .table.dataTable tbody td.active, .table.dataTable tbody tr.active td {
            background-color: red!important;}
      ")),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {
  
  output$mytable = DT::renderDataTable(    
    datatable(mtcars)
  ) 
}
runApp(list(ui = ui, server = server))

0
投票

我发现它可以在 DT 0.30 版本中工作:

library(shiny)
library(DT)
library(bslib)

ui <- fluidPage(
  theme = bs_theme(),
  tags$style(
    HTML("
      .table.dataTable > tbody > tr.selected:hover > td,
      .table.dataTable > tbody > tr.selected > td {
        background-color: red !important;
        box-shadow: 0 0 3px red !important;
      }"
    )
  ),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {
  output$mytable = DT::renderDataTable(    
    datatable(mtcars)
  ) 
}
runApp(list(ui = ui, server = server))
© www.soinside.com 2019 - 2024. All rights reserved.