繁体   English   中英

如何在 R Shiny 中更改 DT Datable header 的背景和文本颜色

[英]how to change background and text color of DT Datable header in R Shiny

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


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)

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

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'});",
        "}")
    ))
  })
  
  
}

-输出

在此处输入图像描述

暂无
暂无

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

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