繁体   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