【问题标题】:how to change background and text color of DT Datable header in R Shiny如何在 R Shiny 中更改 DT Datatable 标题的背景和文本颜色
【发布时间】:2021-08-27 22:27:31
【问题描述】:

我有一个要在 R Shiny 中显示的数据表,但我希望标题列的列名称为红色,文本为白色。使用 formatStyles(),我只能指定整个列,而不仅仅是标题名称行。您建议如何解决这个问题?


library(shiny)
library(dplyr)


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),
    mainPanel(
        DT::DTOutput("table")
    )
))

server <- function(input, output) {
    data <- tibble(name = c("Justin", "Corey", "Sibley"),
           grade = c(50, 100, 100))
    
    output$table <- renderDT({
        datatable(data)
    })

    
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 你的帖子说column names to be red and the text to be in white.。你想要什么背景色

标签: r shiny formatting dt htmlwidgets


【解决方案1】:

如果列名文本为“白色”且背景为“红色”

server <- function(input, output) {
  
  data <- tibble(name = c("Justin", "Corey", "Sibley"),
                 grade = c(50, 100, 100))
  
  output$table <- DT::renderDT({
    datatable(data, options = list(
      
      initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'background-color': 'red', 'color': 'white'});",
        "}")
    ))
  })
  
  
}

-输出

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 2021-09-16
    • 1970-01-01
    • 2018-01-04
    • 2014-03-21
    • 2023-03-04
    • 2021-03-17
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多