英文:
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 <- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论