【问题标题】:Disabling Confirm Button in confirmSweetAlert在 confirmSweetAlert 中禁用确认按钮
【发布时间】:2020-08-28 19:33:02
【问题描述】:

我正在尝试禁用 confirmSweetAlert 中的确认按钮,除非 selectizeInput 中有一些输入。似乎有使用Javascript的解决方案,例如swal.disableConfirmButton()document.getElementsByClassName().disabled = true,但是当我在shinyjs::runjs下运行它们时,这些似乎不起作用。有没有解决这个问题的解决方案?这是我的示例代码:

shinyApp(
  ui <- fluidPage(
    actionButton("button", "Show Sweet Alert!")
  ),

  server <- function(input, output, session) {
    observeEvent(input$button, {
      confirmSweetAlert(
        session = session,
        inputId = "letterSelect",
        title = "Select a Letter!",
        type = "info",
        text = tags$div(
          h4("Please select from the options below then press 'Confirm'.", align = "center"),
          selectizeInput(
            inputId = "letters",
            label = NULL,
            choices = c("A", "B", "C"),
            options = list(placeholder = "None selected."),
            multiple = TRUE,
            width = '100%')
        ),
        closeOnClickOutside = FALSE
      )      
    })
  }

)

【问题讨论】:

    标签: r shiny sweetalert2 shinyjs shinywidgets


    【解决方案1】:

    这似乎有效:

    library(shiny)
    library(shinyWidgets)
    library(shinyjs)
    
    shinyApp(
      ui <- fluidPage(
        useShinyjs(),
        actionButton("button", "Show Sweet Alert!")
      ),
    
      server <- function(input, output, session) {
        observeEvent(input$button, {
          confirmSweetAlert(
            session = session,
            inputId = "letterSelect",
            title = "Select a Letter!",
            type = "info",
            text = tags$div(
              h4("Please select from the options below then press 'Confirm'.", align = "center"),
              selectizeInput(
                inputId = "letters",
                label = NULL,
                choices = c("A", "B", "C"),
                options = list(placeholder = "None selected."),
                multiple = TRUE,
                width = '100%')
            ),
            closeOnClickOutside = FALSE
          )
          runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
        })
    
        observe({
          if(is.null(input$letters)){
            runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
          }else{
            runjs("Swal.getConfirmButton().removeAttribute('disabled');")
          }
        })
      }
    
    )
    

    【讨论】:

    • 非常感谢您的回复!我运行了这段代码,但我仍然没有得到禁用的“确认”按钮。我用上面提到的其他变体替换了 JS 代码,但它似乎仍然不起作用。
    • @m.yoshih 你使用的是什么版本的shinyWidgets?
    • 感谢您指出这一点;我有版本 0.4.8。它现在适用于 0.5.1 的更新版本,尽管 Selectize 下拉菜单出现在按钮后面。
    • 有什么办法可以解决出现在按钮后面的下拉菜单?
    • 我希望做同样的事情,但使用“取消”按钮。所以我在这里写相当于禁用甜蜜警报上的取消按钮,以防其他人需要它runjs("Swal.getCancelButton().setAttribute('disabled', '');")
    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2021-07-03
    • 2014-07-19
    • 2010-11-06
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多