DT :: datatable的条纹 [英] stripes for DT::datatable

查看:76
本文介绍了DT :: datatable的条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在下面的代码中获得数据表的重复模式上色为白色,然后是3,然后再次上色...有人可以帮我提供代码吗?

I am hoping to get a repeated pattern for the datatable in the code below where 3 rows are colored followed by 3 in white, then again colored... Can somebody please help me with code for that?

另外,在我的Shiny应用程序中,有几张桌子,每个桌子可能需要不同的样式。希望能看到一个答案,该答案显示了如何将样式绑定到特定表,以便我可以使用该样式为其他表开发其他样式。

Also, in my Shiny app there are several tables each of which may need different styles. Would appreciate an answer which shows how the style can be tied to a particular table so that I can use that to develop other styles for the other tables.

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函数:

You can try using the following rowCallback funtion:

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] 和使用模运算符为行着色。

Basically you can get the DT row index $(row)["0"]["_DT_RowIndex"] and use the modulo % operator to color rows.

这篇关于DT :: datatable的条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆