R Shiny DataTable选择的行颜色 [英] R Shiny DataTable selected row color
问题描述
我正在尝试为我闪亮的应用程序中的DataTable中的选定行设置突出显示颜色.基本上,我希望所选行的颜色是红色而不是蓝色.但是,我对JavaScript一点都不熟悉,因此我正在努力编写适当的回调(至少我认为是问题所在).到目前为止,这是我尝试过的:
I am trying to set the highlight color for a selected row in a DataTable in my shiny app. Basically I want the color of selected rows to be say red rather than blue. However, I am not at all familiar with JavaScript so I am struggling to write the appropriate callback (at least I think that is the problem). Here is what I've tried so far:
# 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更改为类似以下内容:
As you can see, so far I am just trying to figure out how to change the row colors. Once I had figured this out I was going to try and change the css to something like:
"tr.selected td, table.dataTable td.selected { background-color: #f00}"
但是我还没到那儿-不幸的是,上面的代码对背景颜色没有任何作用.如果有人可以帮助我提供整个解决方案,那就太好了.
But I haven't gotten there yet - unfortunately the code above doesn't do anything to the background color. If anyone could help me with the whole solution that would be great.
推荐答案
这应该可以完成:
#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))
这篇关于R Shiny DataTable选择的行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!