【发布时间】:2017-02-24 22:43:36
【问题描述】:
我在 R 中构建 Shiny 应用程序时遇到了这个问题。在应用程序中,操作按钮用于触发隐藏的下载按钮。这将允许我观察操作按钮事件,对该事件做出反应,然后触发下载过程。
但是,当我将下载按钮的显示属性设置为隐藏按钮的 href 目标时,它通常以 "session/1c47..ef8/download/download_show?w=" 之类的目标为目标。
下面是一个较小的 Shiny 应用程序,它重现了该问题。
shinyApp(
ui = fluidPage(
tags$head(
tags$style(HTML(".hide { display: none; }")),
tags$script(HTML('
Shiny.addCustomMessageHandler("trigger-button", function(message) {
document.getElementById(message.button_id).click();
});
'))
),
div(
class = "disable",
downloadButton("download_shown", "Shown"),
div(
class = "hide",
downloadButton("download_hidden", "Hidden")
)
),
br(),
actionButton("trigger_shown", "I can trigger the visible button!"),
actionButton("trigger_hidden", "I can trigger the hidden button!")
),
server = function(input, output, session) {
output$download_shown <- downloadHandler(
filename = "sample.txt",
content = function(file) {
cat("I'm visible!\n", file = file)
}
)
output$download_hidden <- downloadHandler(
filename = "sample2.txt",
content = function(file) {
cat("I'm hidden!\n", file = file)
}
)
observeEvent(input$trigger_shown, {
session$sendCustomMessage(
"trigger-button",
list(button_id = "download_shown")
)
})
observeEvent(input$trigger_hidden, {
session$sendCustomMessage(
"trigger-button",
list(button_id = "download_hidden")
)
})
}
)
在应用程序中,两个动作按钮触发它们对应的下载按钮。触发可见的下载按钮会导致正确下载文件sample.txt。触发隐藏的下载按钮会导致下载 HTML 文件,即网页,而不是 sample2.txt 文件。此外,如果您检查生成的 HTML,您可以看到 download_hidden 下载按钮有一个没有目标的 href 属性。
- HTML 规范中是否有规定隐藏元素的内容 不能有 href 目标?这似乎不太可能,我的 搜索出现了任何证实这一点的 -one 或 -thing。
- Shiny 在内部会忽略隐藏的元素吗?
- 同时,有没有人建议隐藏按钮
不使用
hidden或display: none;?
提前谢谢你。
【问题讨论】: