formatStyle(..., target = "row") no longer applies row background color (possibly due to bslib or DataTables changes?)
Using `formatStyle(..., target = "row")` to apply a background color to an entire row based on a column value used to work in my shinydashboard-based app. However, after switching the app to use bslib, I recently discovered that the row background color is no longer applied.
Reproducible example:
``` R
library(shiny)
library(bslib)
library(DT)
ui <- fluidPage(
theme = bs_theme(version = 5), # Enable bslib theme
tags$style(HTML("#downloadAnalysis { margin-bottom: 10px; }")),
actionButton("downloadAnalysis", "Download"),
DTOutput("tbl")
)
server <- function(input, output, session) {
dataProject <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Status = c("Yes", "Nei", "Yes"),
stringsAsFactors = FALSE
)
output$tbl <- renderDT({
datatable(
dataProject,
filter = "top",
extensions = "Buttons",
rownames = FALSE,
options = list(
pageLength = 10,
dom = '<"dwnld">lrtip'
)
) %>%
formatStyle(
"Status",
target = "row",
backgroundColor = styleEqual("Nei", "#FFB6C1")
)
})
}
shinyApp(ui, server)
```
It seems that, in the example above, `datatable` renders selected rows using box-shadow instead of background-color, which may be interfering with or overriding the styling applied via target = "row".
I got this fixed by using ` target = "cell",`and ` columns = names(dataProject)`. That said, it feels confusing since the intent is to highlight a row, but the code targets cells instead.
```R
formatStyle( columns = names(dataProject), # Apply to all columns
valueColumns = "Status",
target = "cell",
backgroundColor = styleEqual("Nei", "#FFB6C1"))
```
关闭于 2025-05-02 3 条评论