问题描述
我从这个问题中使用了一块 javascript
:
I'm using a piece of javascript
from this question: SO
它适用于按钮,但我也想禁用 sliderInput
, selectInput
等内容 textInput
。
It works for buttons, but I would also like to disable things like sliderInput
, selectInput
and textInput
as well.
我试图用输入替换按钮,这会禁用 textinput
字段。我想知道是否有办法禁用1 go中的所有元素。
I tried to replace the 'button' with 'input' which does disable the textinput
fields. I am wondering whether there is a way to disable all elements in 1 go.
更大的问题如下:
当你打开下拉按钮
时,关闭按钮通常应该是如果从下面的演示应用程序中删除了javascript标记,请删除模式对话框
。但是,当脚本在应用程序中时,关闭按钮由于某种原因不再起作用。它仍会打印文本命令,这意味着它被观察到,但模态不会关闭。对话框中的另一个按钮仍然可以正常工作。
The bigger problem is the following:When you open the dropdownbutton
, the close button normally should remove the modal dialog
in case the javascript tag is removed from the demo app below. However, when the script is in the app, the close button doesn't work anymore for some reason. It still prints the text command, meaning it is observed, but the modal doesn't close. The other button in the dialog still works normally.
应用程序:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
h3('Disable buttons while running'),
actionButton('btn_run','Run long process'),
hr(),
h3('Inputs'),
actionButton('btn1','Button 1'),
hr(),
textInput('text1', 'Text1',"my text:"),
hr(),
selectInput('select1', 'Selectinput', choices = c('A', 'B', 'C'), selected = 'A'),
hr(),
h5('Dropdown'),
dropdownButton(inputId = "MyDropDown",
h3("This is a dropdown"),
actionButton('btn_run2','Run other long process'),
fluidRow(actionButton( "CloseDropDown", "Close"), style = "float: right; margin-right:10px"),
icon = icon("tasks"),
tooltip = tooltipOptions(title = "Click to open"), width = "500px"),
hr(),
sliderInput('slid3','Slider 1',min=0,max=1,value=0.5),
tags$script(HTML("$(document).on('shiny:busy', function() {
var inputs = document.getElementsByTagName('button');
console.log(inputs);
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = true;
}
});
$(document).on('shiny:idle', function() {
var inputs = document.getElementsByTagName('button');
console.log(inputs);
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
})" ))
)
server <- function(input, output, session){
observeEvent(input$btn_run,{
Sys.sleep(5)
})
observeEvent(input$btn_run2,{
Sys.sleep(5)
})
observeEvent(input$CloseDropDown, {print('closing?')
toggleDropdownButton(inputId = 'MyDropDown') })
}
shinyApp(ui = ui, server = server)
推荐答案
我无法帮助你关闭按钮,虽然我发现只要你设置 shiny:idle
句柄,任何对JavaScript的调用都会触发闪亮:空闲
因此运行处理程序而不是 toggleDropdownButton 。
I cannot help you with the close button, though I found out that as soon as you set your
shiny:idle
handle, any call to JavaScript will fire shiny:idle
and hence runs the handler instead of the JavaScript code behind toggleDropdownButton
.
然而,如何在JavaScript中选择多个元素的第一个问题可以通过一点
jQuery来解决
。将您的代码更改为
However, the first question of how to select more than one element in your JavaScript can be solved with a bit of
jQuery
. Change your code to
$(document).on('shiny:busy', function() {
var $inputs = $('button,input');
console.log($inputs);
$inputs.prop('disabled', true);
});
$(document).on('shiny:idle', function() {
var $inputs = $('button,input');
console.log($inputs);
$inputs.prop('disabled', false);
})
用它可以选择按钮和文本输入。现在你可以自己找出用来禁用下拉列表的代码。
With that you can select buttons and the text input. Now you can find out yourself which code to use to also disable the dropdown.
BTW:也许你想看看
shinyjs :: disable
。使用此功能,您可以从 R
侧禁用控件。你可以把它放在长计算的开头,并在最后使用 shinyjs :: enable
。
BTW: maybe you want to look at
shinyjs::disable
. With this function you can disable your controls from the R
side. You would put that in the beginning of your long calculation and use shinyjs::enable
at the end.
这篇关于Shiny忙时禁用元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!