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

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

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.

  1. library(shiny)
  2. showMyModal <- function() {
  3. showModal(
  4. modalDialog(
  5. verbatimTextOutput("mytext"),
  6. footer = tagList(
  7. modalButton("Dismiss"),
  8. )
  9. )
  10. )
  11. }
  12. ui <- fluidPage(
  13. actionButton("openDialog", "Open dialog")
  14. )
  15. server <- function(input, output, session) {
  16. observeEvent(input$openDialog, {
  17. showMyModal()
  18. })
  19. output$mytext <- renderPrint({
  20. input$openDialog
  21. })
  22. }
  23. 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,直到模态框已经(重新)渲染:

  1. library(shiny)
  2. showMyModal <- function() {
  3. showModal(
  4. conditionalPanel("input.modal_visible == true",
  5. modalDialog(
  6. verbatimTextOutput("mytext"),
  7. footer = tagList(
  8. modalButton("Dismiss"),
  9. )
  10. )
  11. )
  12. )
  13. }
  14. ui <- fluidPage(
  15. tags$script(HTML(
  16. "$(document).on('shown.bs.modal','#shiny-modal', function () {
  17. Shiny.setInputValue(id = 'modal_visible', value = true);
  18. });
  19. $(document).on('hidden.bs.modal','#shiny-modal', function () {
  20. Shiny.setInputValue(id = 'modal_visible', value = false);
  21. });"
  22. )),
  23. actionButton("openDialog", "Open dialog")
  24. )
  25. server <- function(input, output, session) {
  26. observeEvent(input$openDialog, {
  27. showMyModal()
  28. })
  29. output$mytext <- renderPrint({
  30. input$openDialog
  31. })
  32. }
  33. 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:

  1. library(shiny)
  2. showMyModal &lt;- function() {
  3. showModal(
  4. conditionalPanel(&quot;input.modal_visible == true&quot;,
  5. modalDialog(
  6. verbatimTextOutput(&quot;mytext&quot;),
  7. footer = tagList(
  8. modalButton(&quot;Dismiss&quot;),
  9. )
  10. )
  11. )
  12. )
  13. }
  14. ui &lt;- fluidPage(
  15. tags$script(HTML(
  16. &quot;$(document).on(&#39;shown.bs.modal&#39;,&#39;#shiny-modal&#39;, function () {
  17. Shiny.setInputValue(id = &#39;modal_visible&#39;, value = true);
  18. });
  19. $(document).on(&#39;hidden.bs.modal&#39;,&#39;#shiny-modal&#39;, function () {
  20. Shiny.setInputValue(id = &#39;modal_visible&#39;, value = false);
  21. });&quot;
  22. )),
  23. actionButton(&quot;openDialog&quot;, &quot;Open dialog&quot;)
  24. )
  25. server &lt;- function(input, output, session) {
  26. observeEvent(input$openDialog, {
  27. showMyModal()
  28. })
  29. output$mytext &lt;- renderPrint({
  30. input$openDialog
  31. })
  32. }
  33. 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:

确定