簡體   English   中英

如何在 shinyWidgets 中使用 callbackJS?

[英]How to use callbackJS in shinyWidgets?

我曾經在 Shinyalert 中使用過,但是因為我嘗試在 shinyWidgets (ask_confirmation) 中使用一些錯誤。 在 Shinyalert 中,我使用 callbackJS 來了解用戶是單擊 OK 還是單擊 Cancel,但它在 ask_confirmation 中不起作用。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
)

server <- function(input, output, session) {
  ask_confirmation(
    "form1",
    title = " ",
    type = "question",
    btn_labels = c("Cancel", "OK"),
    allowEscapeKey = TRUE,
    closeOnClickOutside = TRUE,
    html = TRUE,
    #######################
    #this one is not woking
    callbackJS =      "  
    function(x) {
      if (x !== false) {
      alert('t');
      }
    else{
    alert('ttt');
    }
}
    
    ",
    ######################
    text =  
      div(HTML("
    
<form style = ' display:flex; flex-direction: row; justify-content: center; align-items: center' action=''>
<div style='border: 5px  black;'>
<input type='text'name='add_nosha_tohen' autocomplete='off' id='add_nosha_tohen' value=''   dir='rtl' style='border-color: black; font-size: 12px;height: 45px; width: 400px; display: block;'/></input>
</div>
</form>
"
      ))
  )
}

shinyApp(ui, server)

知道如何解決嗎?

ask_confirmation中沒有callbackJS參數。 要進行回調並運行 JS 代碼,您可以執行以下操作:

library(shiny)
library(shinyWidgets)
library(shinyjs)
ui <- fluidPage(
    useShinyjs()
)

server <- function(input, output, session) {
    ask_confirmation(
        "form1",
        title = " ",
        type = "question",
        btn_labels = c("Cancel", "OK"),
        allowEscapeKey = TRUE,
        closeOnClickOutside = TRUE,
        html = TRUE,
######################
text =  
    div(HTML("
<form style = ' display:flex; flex-direction: row; justify-content: center; align-items: center' action=''>
<div style='border: 5px  black;'>
<input type='text'name='add_nosha_tohen' autocomplete='off' id='add_nosha_tohen' value=''   dir='rtl' style='border-color: black; font-size: 12px;height: 45px; width: 400px; display: block;'/></input>
</div>
</form>
"
    ))
    )
    
    observeEvent(input$form1, {
        if(is.null(input$form1)) runjs("alert('NULL');")
        else if (input$form1) runjs("alert('true');")
        else runjs("alert('false');")
    }, ignoreNULL = TRUE)
}

shinyApp(ui, server)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM