在confirmSweetAlert中禁用确认按钮。

问题描述 投票:0回答:1

我想禁用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个回答
0
投票

这似乎是可行的。

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');")
      }
    })
  }

)
© www.soinside.com 2019 - 2024. All rights reserved.