使用 bslib 时覆盖 DT 默认选择颜色

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

这个问题与R DT override default choice color非常相似,使用了部分代码,但最重要的是我想使用https://github.com/rstudio/bslib进行样式设置。

我想在 DT 表中选择一行时覆盖默认颜色,但失败了几个小时。

这是一些要使用的代码

library(shiny)
library(DT)

library(bslib)

color_sel <-"#7cfc00" 

ui <- fluidPage(
  theme = bslib::bs_theme(version = 4,
                          bootswatch = "flatly",
                        #  bootswatch = NULL,
                          primary = '#00488E',
                          secondary = '#4AB9FA',
                          success = "#4AB9FA",
                          info = "#4AB9FA"
  ) %>% 
  bs_add_variables(
    "body-bg" = "#EEEEEE",
    "font-family-base" = "monospace",
    "table_active_bg" = color_sel
  )
  ,
#  tags$style(HTML(paste0("table.dataTable tbody tr.selected td, table.dataTable td.selected{background-color: ", color_sel," !important;}"))),
  fluidRow(dataTableOutput("tbl"))
)
server <- function(input, output){
  output$tbl <- renderDataTable({
    datatable(mtcars)
  })
  
}

app <- shinyApp(ui = ui, server= server)
runApp(app)

如您所见,我也尝试了两个地方用漂亮的绿色覆盖标准蓝色,但它不起作用。 bs_add_variables() 正在工作,由 body-bg 和 font-family-base 检查。怎么了?

Still blue row selection color (default)

r shiny dt bslib
1个回答
3
投票

实际上,多一点搜索就能找到这个解决方案。将这段 CSS 添加到您的应用程序中:

table.dataTable tbody tr.active td {
    background: pink !important;
}
© www.soinside.com 2019 - 2024. All rights reserved.