DT::datatable 的条纹

stripes for DT::datatable

我希望在下面的代码中获得 datatable 的重复模式,其中 3 行是彩色的,然后是白色的 3 行,然后再次是彩色的...有人可以帮我编写代码吗?

此外,在我的 Shiny 应用程序中有多个 tables,每个可能需要不同的样式。将不胜感激显示样式如何与特定 table 相关联的答案,以便我可以使用它为其他 table 开发其他样式。

require(shiny)
require(DT)

MkDF <- function(nr) { data.frame(value1=1:nr, value2=runif(nr)) }

server <- function(input, output) {
    ds <- MkDF(15)
    output$tbl = DT::renderDataTable({ 
        DT::datatable(ds, options=list(info=F, searching=F, paging=F),
            container= htmltools::tags$table(class="stripe row-border"),
            colnames=c("My Value1","My Value2")) %>% formatRound(2,2)
    })
}

ui <- shinyUI(
    navbarPage("Example",
        tabPanel("DT", fluidRow( column(offset=2, width=4, DT::dataTableOutput('tbl') ) ) )
    )
)

shinyApp(ui=ui, server=server)

您可以尝试使用以下 rowCallback 函数:

DT::datatable(ds,
 options=list(info=F, searching=F, paging=F,
 rowCallback=JS(
  'function(row,data) {
     if($(row)["0"]["_DT_RowIndex"] % 6 <3) 
            $(row).css("background","orange")
   }'))) %>% formatRound("value2",2)  

基本上你可以获得 DT 行索引 $(row)["0"]["_DT_RowIndex"] 并使用取模 % 运算符为行着色。