折叠行组闪亮 [英] Collapse rowGroup Shiny

查看:74
本文介绍了折叠行组闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序(如下),我尝试使用具有折叠组功能的DataTable输出分组表.我发现在jQuery 此处中实现的解决方案,但我不知道它有多复杂实施可以移到R中.

I have rather simple application (below) where I try to output grouped table using DataTable with the ability to collapse the groups. I found solution in that is implemented in jQuery here but I have no idea how such complex implementation could be moved into R.

目前,我可以在一个小组中崩溃,但不能崩溃整个小组本身.有什么提示可以在Shiny中实现吗?

Currently, I am able to collapse within a group but not the whole group itself. Any hints how this could be implemented in Shiny?

我的应用程序:

library(shiny)
library(DT)
library(shinyjs)

ui <- fluidPage(

   # Application title
   titlePanel("Collapse/Expand table"),

            mainPanel(
          DTOutput("my_table")

      )
   )


server <- function(input, output) {

    output$my_table<-DT::renderDataTable({

        datatable(mtcars[1:15,1:5],
                  extensions = 'RowGroup', 
                  options = list(rowGroup = list(dataSrc=c(3)),
                                 pageLength = 20),
                  callback = JS("
                                table.on('click', 'tr', function () {
                                    var rowsCollapse = $(this).nextUntil('.group');
                                    $(rowsCollapse).toggleClass('hidden');
                                 });"))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

编辑

考虑到AEF注释,可以调整代码以指定甚至在单击表 body 后必须进行的操作.实际上,这会折叠所有行,直到下一组为止.剩下的部分是仅将点击限制在组行上. 回调现在应该是:

Given AEF comment one can adjust the code to specify that even has to take place once table body is clicked. This indeed collapses any rows until next group. The remaining part is to restrict the clicks only onto group rows. The callback should be now:

callback = JS("$('#DataTables_Table_0 tbody').on('click', 'tr', function () {
 $(this).nextUntil('.group').toggleClass('hidden');});"))

推荐答案

原来是DT的javascript代码的错误.有一个单击事件侦听器,它将记录所有单击的单元格的信息.但是,RowGroup扩展会创建一个不属于原始数据集的新行,并导致错误. 此错误停止了进一步的javascript执行.

It turns out to be a bug of DT's javascript code. There's a click event listener that will record all the info of the clicked cells. However, the RowGroup extension creates a new row that doesn't belong to the original datasets and leads to an error. This error stops the further javascript executions.

在您的情况下,由于上一个单元格单击事件引发了错误,因此tr.group事件不起作用.

In your cases, the tr.group event doesn't work because of the error thrown from the previous cell-click event.

我们已经修复了该错误,并且DT的开发版本应可与以下代码配合使用:

We've fixed this bug and the dev version of DT should work with the below code:

library(shiny)
library(DT)
ui <- fluidPage(# Application title
  titlePanel("Collapse/Expand table"),
  mainPanel(DTOutput("my_table")))

callback_js <- JS(
  "table.on('click', 'tr.dtrg-group', function () {",
  "  var rowsCollapse = $(this).nextUntil('.dtrg-group');",
  "  $(rowsCollapse).toggleClass('hidden');",
  "});"
)

server <- function(input, output) {
  output$my_table <- DT::renderDT({
    datatable(
      mtcars[1:15, 1:5],
      extensions = 'RowGroup',
      options = list(rowGroup = list(dataSrc = 3), pageLength = 20),
      callback = callback_js,
      selection = 'none'
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)

再次感谢您的举报!

DT的Github问题的代码: https://github.com/rstudio/DT /issues/759

Ticker to the Github issue of DT: https://github.com/rstudio/DT/issues/759

这篇关于折叠行组闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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