更新模态框界面,然后打开模态框。

huangapple go评论65阅读模式
英文:

update modal UI before opening the modal

问题

The Chinese translation of the provided text is as follows:

从第二次点击打开模态框的按钮开始,模态框首先显示旧数据,然后触发渲染函数的更新。

"Is there a smart way to first update the modal UI and only afterwards opening the modal? I am looking for a way that the modal does not show outdated data before it gets refreshed."

英文:

From the second time on when you click the button that opens the modal the modal opens showing the old data at first and then the update of the render-function is triggered.

library(shiny)

showMyModal <- function() {
  showModal(
    modalDialog(
      verbatimTextOutput("mytext"),
      footer = tagList(
        modalButton("Dismiss"),
      )
    )
  )
}

ui <- fluidPage(
  actionButton("openDialog", "Open dialog")
)

server <- function(input, output, session) {
  
  observeEvent(input$openDialog, {
    showMyModal()
  })
  
  output$mytext <- renderPrint({
    input$openDialog
  })
  
}

shinyApp(ui = ui, server = server)

Is there a smart way to first update the modal UI and only afterwards opening the modal? I am looking for a way that the modal does not show outdated data before it gets refreshed.

答案1

得分: 1

这是利用我之前的答案这里的方法。

以下部分阻止显示您的modalDialog,直到模态框已经(重新)渲染:

library(shiny)

showMyModal <- function() {
  showModal(
    conditionalPanel("input.modal_visible == true",
                     modalDialog(
                       verbatimTextOutput("mytext"),
                       footer = tagList(
                         modalButton("Dismiss"),
                       )
                     )
    )
  )
}

ui <- fluidPage(
  tags$script(HTML(
    "$(document).on('shown.bs.modal','#shiny-modal', function () {
       Shiny.setInputValue(id = 'modal_visible', value = true);
      });
     $(document).on('hidden.bs.modal','#shiny-modal', function () {
       Shiny.setInputValue(id = 'modal_visible', value = false);
     });"
  )),
  actionButton("openDialog", "Open dialog")
)

server <- function(input, output, session) {
  observeEvent(input$openDialog, {
    showMyModal()
  })
  
  output$mytext <- renderPrint({
    input$openDialog
  })
}

shinyApp(ui = ui, server = server)
英文:

This is an approach utilizing my earlier answer here.

The following is blocking the display of your modalDialog until the modal has been (re)rendered:

library(shiny)

showMyModal &lt;- function() {
  showModal(
    conditionalPanel(&quot;input.modal_visible == true&quot;,
                     modalDialog(
                       verbatimTextOutput(&quot;mytext&quot;),
                       footer = tagList(
                         modalButton(&quot;Dismiss&quot;),
                       )
                     )
    )
  )
}

ui &lt;- fluidPage(
  tags$script(HTML(
    &quot;$(document).on(&#39;shown.bs.modal&#39;,&#39;#shiny-modal&#39;, function () {
       Shiny.setInputValue(id = &#39;modal_visible&#39;, value = true);
      });
     $(document).on(&#39;hidden.bs.modal&#39;,&#39;#shiny-modal&#39;, function () {
       Shiny.setInputValue(id = &#39;modal_visible&#39;, value = false);
     });&quot;
  )),
  actionButton(&quot;openDialog&quot;, &quot;Open dialog&quot;)
)

server &lt;- function(input, output, session) {
  observeEvent(input$openDialog, {
    showMyModal()
  })
  
  output$mytext &lt;- renderPrint({
    input$openDialog
  })
}

shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年4月19日 21:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定