簡體   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