R Shiny DataTable选择的行颜色

詹姆斯·阿灵厄姆

我正在尝试为我闪亮的应用程序中的DataTable中的选定行设置突出显示颜色。基本上,我希望所选行的颜色是红色而不是蓝色。但是,我对JavaScript一点都不熟悉,因此我正在努力编写适当的回调(至少我认为是问题所在)。到目前为止,这是我尝试过的:

# ui.R
library(shiny)

fluidPage(
  title = 'DataTables Test',
  DT::dataTableOutput('table')
)

# server.R
library(shiny)
library(DT)

# render the table
output$table = renderDataTable(datatable(head(iris, 20), 
options = list(
    initComplete = JS(
      "function(settings, json) {",
      "var rows = $(this.api().table().rows());",
      "for (var i = 0; i < rows.length; i++){ ",
      "var row = rows[i];",
      "row.css({'background-color': '#000', 'color': '#f00'})",
      "}",
      "}")
  )))

})

如您所见,到目前为止,我只是想弄清楚如何更改行颜色。一旦弄清楚了,我将尝试将css更改为以下内容:

"tr.selected td, table.dataTable td.selected { background-color: #f00}"

但是我还没到那儿-不幸的是,上面的代码对背景颜色没有任何作用。如果有人可以帮助我提供整个解决方案,那将是很好的。

猪排

这应该做的工作:

#rm(list = ls())
library(shiny)
library(DT)

ui <- basicPage(
  tags$style(HTML('table.dataTable tr.selected td, table.dataTable td.selected {background-color: pink !important;}')),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {

  output$mytable = DT::renderDataTable(    
    datatable(mtcars)
  ) 
}
runApp(list(ui = ui, server = server))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章