简体   繁体   English

R Shiny DT 数据表如何更改选定的类

[英]R Shiny DT datatable how can I change the selected class

Is there a way to change the class of the selected row/column/cell without having to use the "Select" extension?有没有办法更改所选行/列/单元格的类而无需使用“选择”扩展?

The problem: when style = "bootstrap" is set, the class for the selected item is class = "active" .问题:设置style = "bootstrap"时,所选项目的类是class = "active" Because bootstrap themes usually set active to some sort of grey, DT must override bootstrap's active background color so it's not confused with the white/grey alternating row colors.因为引导主题通常将active设置为某种灰色,所以 DT 必须覆盖引导的active背景颜色,这样它就不会与白色/灰色交替的行颜色混淆。 Instead of overriding the color, I want to change the class from active to info .我不想覆盖颜色,而是想将类从active更改为info This way, any theme I choose will look nice and the selected row will not be confused with the not-selected rows.这样,我选择的任何主题都会看起来不错,并且所选行不会与未选择的行混淆。

Additionally, the blue that DT chose to override bootstrap's active background color makes the highlight of search terms look hideous.此外,DT 选择覆盖 bootstrap 的active背景颜色的蓝色使搜索词的突出显示看起来很可怕。 See the example.请参阅示例。 And the blue that DT chose for the style="default" does not always look good with the selected bootstrap theme. DT 为style="default"选择的蓝色在所选引导主题中并不总是很好看。

BTW, I successfully changed the selected class to info using the "Select" extension and setting it in the options, but that presented other problems, mainly that I could not pre-select a row.顺便说一句,我使用“选择”扩展名成功地将所选类更改为info并将其设置在选项中,但这会带来其他问题,主要是我无法预先选择一行。

library(shiny)
library(DT)

dt <- data.frame(colA = sample(c("one","two"),10,replace=T), ColB = rnorm(10))

ui <- basicPage(
  DTOutput("test_table")
)

server <- function(input, output) {
  
  output$test_table <- renderDT({
    datatable(dt,
      style = "bootstrap",
      selection = list(mode = "single", selected = 1),
      options = list(searchHighlight = T, search = list(search = "o"))
    )
  })
}

shinyApp(ui = ui, server = server)

I found a way to solve this problem by using the bslib package.我找到了一种通过使用bslib包来解决这个问题的方法。 bslib loads the bootstrap version you want. bslib加载您想要的bootstrap版本。 If you load version 4 or 5 you can then refer to the :root css custom property variables bootstrap creates, such as --bs-info .如果您加载版本 4 或 5,则可以参考bootstrap创建的:root css 自定义属性变量,例如--bs-info You can either overwrite or use the variable's value.您可以覆盖或使用变量的值。 See mozzilas' help on how to use css custom properties有关如何使用 css 自定义属性,请参阅 mozzilas 的帮助

You could set the color of the selected row to variables such as --bs-primary or --bs-warning .您可以将所选行的颜色设置为变量,例如--bs-primary--bs-warning Here is a list of all the bootstrap css variables这是所有引导 css 变量的列表

In the following example, I'm setting the class active to have background-color = var(--bs-primary) .在以下示例中,我将active类设置为具有background-color = var(--bs-primary) The selected row will show the primary color for any theme selected.所选行将显示所选主题的原色。 Alas, NO HARD CODING CSS!唉,没有硬编码 CSS! In the event the user choses the theme, the selected row will always adhere to the theme's colors.如果用户选择了主题,则所选行将始终遵循主题的颜色。

library(shiny)
library(DT)

dt <- data.frame(colA = sample(c("one","two"),10,replace=T), ColB = rnorm(10))

ui <- fluidPage(
  theme = bslib::bs_theme(version = 5, bootswatch = "sandstone"),
  tags$head(tags$style(HTML("
      .table.dataTable tbody td.active, .table.dataTable tbody tr.active td {
            background-color: var(--bs-primary)!important;}
      "))),
  
  DTOutput("test_table")
)

server <- function(input, output) {
  
  output$test_table <- renderDT({
    datatable(dt,
              style = "auto",
              selection = list(mode = "single", selected = 2),
              options = list(searchHighlight = T, search = list(search = "o"))
    )
  })
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM