【问题标题】:How to use ColVis plugin in DT to hide multiple columns in a custom container?如何在 DT 中使用 ColVis 插件隐藏自定义容器中的多个列?
【发布时间】:2018-04-04 11:57:20
【问题描述】:

我有一个闪亮的应用程序,我在其中使用 DT 库来可视化表格。我正在使用容器来呈现表格。

sketch = htmltools::withTags(table(
class = 'display',
thead(
  tr(
    th(rowspan = 2, 'Name'),
    th(rowspan = 2, 'Description'),
    th(rowspan = 2, 'From'),
    th(rowspan = 2, 'To'),
    th(rowspan = 2, 'Duration'),
    th(colspan = 3, 'Rain', style="text-align:center"),
    th(colspan = 3, 'Flow', style="text-align:center"),
    th(colspan = 3, 'Depth', style="text-align:center"),
    th(colspan = 3, 'Velocity', style="text-align:center")
  ),
  tr(
    lapply(rep(c('Min', 'Max', 'Mean'), 4), th)
  )
)))
  output$table2 <- DT::renderDataTable({
    datatable(eventSelection[[input$options]], class = 'cell-border stripe table-hover', rownames = FALSE, container = sketch,
              extensions = 'Buttons', options = list(scrollX = TRUE, dom = 'Bfrtip', buttons = I('colvis'))
              ) %>%
      formatDate(c(3,4), method='toLocaleString') %>%
      formatRound(5:17, digits = 3)
  })

表格如下所示:

我希望在列可见性选项卡中拥有 Rain、Flow、Depth、Velocity 选项,并且能够在我单击它们时隐藏相应的 Min、Max 和 Mean 统计列。最好的方法是什么?

谢谢!

【问题讨论】:

    标签: javascript r datatables shiny dt


    【解决方案1】:

    我可以通过使用数据表文档来做到这一点。

    代码如下:

      sketch = htmltools::withTags(table(
    class = 'display',
    thead(
      tr(
        th(rowspan = 2, 'Name'),
        th(rowspan = 2, 'Description'),
        th(rowspan = 2, 'From'),
        th(rowspan = 2, 'To'),
        th(rowspan = 2, 'Duration'),
        th(colspan = 3, 'Rain', style="text-align:center"),
        th(colspan = 3, 'Flow', style="text-align:center"),
        th(colspan = 3, 'Depth', style="text-align:center"),
        th(colspan = 3, 'Velocity', style="text-align:center")
      ),
      tr(
        lapply(rep(c('Min', 'Max', 'Mean'), 4), th)
      )
    )))
    
    
    
     button_list <- list(list(extend='colvisGroup', text="Rain", show=c(5:7)),
                          list(extend='colvisGroup', text="Flow", show=c(8:10)),
                          list(extend='colvisGroup', text="Depth", show=c(11:13)),
                          list(extend='colvisGroup', text="Velocity", show=c(14:16)),
                          list(extend='colvisGroup', text="Hide all", hide=c(5:16))
                          )
    
      output$table2 <- DT::renderDataTable({
        datatable(eventSelection[[input$options]], class = 'cell-border stripe table-hover', rownames = FALSE, container = sketch,
                  extensions = 'Buttons', options = list(scrollX = TRUE, dom = 'Bfrtip',
                                                         columnDefs = list(list(visible=FALSE, targets=c(5:16))),
                                                         buttons = button_list)
                  ) %>%
          formatDate(c(3,4), method='toLocaleString') %>%
          formatRound(5:17, digits = 3)
      })
    

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多