在shiny中,DT::datatable的背景颜色是什么?

7
我该如何在shiny应用中更改datatable的选定行的背景颜色?我的ui.R代码如下:
library(shinydashboard)

header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
  )
)
body <- dashboardBody(
  fluidRow(
    column(width = 6,
           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table1')
           ),
           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table2')
           )
    ),
    column(width = 6,
           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table3')
           )
    )
  )
)
dashboardPage(header, sidebar, body)
1个回答

4
您可以使用CSS来实现这个目标。在jquery.min.dataTables.css中,通过table.dataTable tbody tr.selected { background-color: #B0BED9;}设置所选行的背景颜色。
以下是基于您的代码示例的最简示例,展示如何使用tags$style进行覆盖:
library(shinydashboard)

header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
  )
)
body <- dashboardBody(
  tags$head(tags$style(HTML("#table1 tr.selected, #table2 tr.selected {background-color:red}"))),
  fluidRow(
    column(width = 6,
           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table1')
           ),
           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table2')
           )
    )
  )
)
ui<-dashboardPage(header, sidebar, body)

server = function(input, output) {
  output$table1 = DT::renderDataTable(
    iris, options = list(lengthChange = FALSE)
  )

  output$table2 = DT::renderDataTable(
    iris, options = list(lengthChange = FALSE)
  )
}

shinyApp(ui,server)

我添加了表格ID(#table1 tr.selected, #table2 tr.selected),以使此选择器比默认选择器更具优先级并覆盖它,关于此的更多信息请点击这里


你能否更新你的回答,使其适用于 shinydashboard?我在 fluidRowcolumn 中的 box 内有这个表格。 - danas.zuokas
1
也许您可以更新一下您的问题,加入一些代码?我不确定您的应用程序是什么样子的。 - NicE
奇怪的是,当我运行你的示例应用程序时,它没有任何影响。 - danas.zuokas
也许可以发布 sessionInfo() 的输出结果,我的猜测是我们的软件包版本不同。 - NicE
好的,我已经弄明白了。如@NicE提供的链接所指出的那样,在颜色规范旁边添加!important - danas.zuokas
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接