我正在尝试更改R Shiny App中选择的数据表的bg颜色。编写了相同的CSS代码,但无法覆盖现有的CSS。任何解决方法来实现它。
这是我的代码:
library(DT)
library(shiny)
library(shinydashboard)
library(shinyjs)
shinyUI(
dashboardPage (
dashboardHeader(title="Report"),
dashboardSidebar(sidebarMenu(menuItem("Table",tabName="Table"))),
dashboardBody(
tags$head(tags$style(HTML("
#DataTable tr.selected {background-color:cyan !important;}
table.dataTable.hover tbody tr:hover, table.dataTable.display tbody tr:hover {
background-color: rgb(143,209,63) !important;
}
.odd {
background-color : rgb(173,219,241) !important;
}
.even {
background-color : rgb(232,245,251) !important;
}
"))),
useShinyjs() ,
tabItems(
tabItem(tabName = "Table",
DT::dataTableOutput("DataTable")
)
))
))
shinyServer(function(input, output) {
output$DataTable <- DT::renderDataTable({
datatable(iris,rownames=FALSE,selection = 'single',options = list(
searching = FALSE,ordering=FALSE,
dom = 'Bfrtip',
buttons = c('copy','excel', 'pdf', 'print', 'colvis'),
columnDefs = list(list(visible=FALSE, targets=c(2))),
rowCallback = JS(
"function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
"var full_text = aData[2]",
# Tooltip for the rows
"$('td:eq(1)', nRow).attr('title', full_text);",
# Showing a hand as a cursor
"$('td:eq(1)', nRow).css('cursor','pointer');",
"$('td:eq(1)', nRow).css('font-weight','bold');",
"}")
)
)
})
答案 0 :(得分:1)
以下是更新的代码:
shinyUI(
dashboardPage (
dashboardHeader(title="Report"),
dashboardSidebar(sidebarMenu(menuItem("Table",tabName="Table"))),
dashboardBody(
tags$style(HTML("
table.dataTable tr.selected td,
table.dataTable td.selected {
background-color: rgb(143,209,63) !important;
}
")),
tags$head(tags$style(HTML("
table.dataTable.hover tbody tr:hover, table.dataTable.display tbody tr:hover {
background-color: rgb(143,209,63) !important;
}
.odd {
background-color : rgb(173,219,241) !important;
}
.even {
background-color : rgb(232,245,251) !important;
}
"))),
useShinyjs() ,
tabItems(
tabItem(tabName = "Table",
DT::dataTableOutput("DataTable")
)
))
))
shinyServer(function(input, output) {
output$DataTable <- DT::renderDataTable({
datatable(iris,rownames=FALSE,selection = 'single',options = list(
searching = FALSE,ordering=FALSE,
dom = 'Bfrtip',
buttons = c('copy','excel', 'pdf', 'print', 'colvis'),
columnDefs = list(list(visible=FALSE, targets=c(2))),
rowCallback = JS(
"function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
"var full_text = aData[2]",
# Tooltip for the rows
"$('td:eq(1)', nRow).attr('title', full_text);",
# Showing a hand as a cursor
"$('td:eq(1)', nRow).css('cursor','pointer');",
"$('td:eq(1)', nRow).css('font-weight','bold');",
"}")
)
)
})