隐藏时如何获取DT :: datatable更新 [英] How to get DT::datatable to update when hidden

查看:74
本文介绍了隐藏时如何获取DT :: datatable更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

datatable s隐藏在Shiny中时,即使 outputOptions(output,'tableID',suspendedWhenHidden = FALSE),它们似乎也不会更新。 被使用。以下是MRE。如果隐藏数据表,然后添加或删除行,然后显示数据表,则会看到它没有更新。但是,只要在数据表可见时对其进行修改,所有过去的更改都会随之赶上。我们如何强制数据表 s在隐藏时重绘?

When datatables are hidden in Shiny, they seem to not update, even if outputOptions(output, 'tableID', suspendWhenHidden=FALSE) is used. Below is an MRE. If you hide the datatable, and then add or delete rows, and then show the datatable, you'll see that it's not updated. However, as soon as you modify the datatable while it's visible, all past changes "catch up" with it. How can we force datatables to redraw themselves while they're hidden?

#ui
shinyUI(
  #tags$head(tags$script(src="main.js")),
  fluidPage(    
    titlePanel("Modifying Hidden Datatable"),
    sidebarLayout(      
      sidebarPanel(
        actionButton("decrease", "Decrease Row Number"),
        actionButton("increase", "Increase Row Number"),
        actionButton("hide", "Hide Data Table"),
        actionButton("show", "Show Data Table")
      ),
      mainPanel(
        tags$head(tags$script(src="main.js")),
        DT::dataTableOutput("df")  
      )
    )
  )
)

#server
library(shiny)
library(DT)

shinyServer(function(input, output, session) {

  values = reactiveValues(n = 1)

  observeEvent(input$decrease, {
    values$n = values$n - 1
    if (values$n == 0) values$n = 1
  })

  observeEvent(input$increase, {
    values$n = values$n + 1
    if (values$n > nrow(mtcars)) values$n = nrow(mtcars)
  })

  output$df = renderDataTable({
    df = mtcars[1:values$n, ]
    DT::datatable(df)
  })
  outputOptions(output, "df", suspendWhenHidden=FALSE)
})

#javascript
$(document).ready(function() {
  $('#hide').click(function() {
    $('#df').hide();
  });
  $('#show').click(function() {
    $('#df').show();
  });
});


推荐答案

事实证明,如果您已经在使用Javascript ,您只需在表格上调用 draw 函数。以下Javascript代码应该有效:

It turns out that if you're already using Javascript, you can just call the draw function on the table. The following Javascript code should work:

$(document).ready(function() {
  $('#hide').click(function() {
    $('#df').hide();
  });
  $('#show').click(function() {
    $('#df').show();
    $("#df table").DataTable().draw();
  });
});

这篇关于隐藏时如何获取DT :: datatable更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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