javascript - sweetalert2 文本输入,并在 R Shiny 中使用 javascript 验证文本

标签 javascript r shiny sweetalert2

我正在 R Shiny 中从sweetalert升级到sweetalert2,到目前为止已经成功地获得了R Shiny 服务器对警报消息中的确定/取消按钮的定期响应,但现在我陷入了下一种类型,即我用来执行一些验证的文本输入消息:

  • 特殊字符的使用 在将值发送到 R 之前。

Here您可以找到一个实现 sweetalert2 的应用程序。

在新问题中,我尝试用包含输入消息的消息替换该应用程序中的 javascript:

myjava <- "shinyjs.swalFromButton = function(params) { 
      var defaultParams = {
    title: null,
    html : null
    };
    params = shinyjs.getParams(params, defaultParams);
    swal({title : params.title, html : params.html,  
    input: 'text',
    showCancelButton : true,
    showConfirmButton : true,
    closeOnConfirm: false,
    confirmButtonColor: '#339FFF',
    allowOutsideClick: false,
    inputValidator: function(value) {
    if(value === '') { return !value && 'You need to write something!'}
    else {
    var val2= true; 
    Shiny.setInputValue('option2', val2, {priority: 'event'}) };
    }
    });
    };"

到目前为止,这有效,但我不知道如何添加其他检查以检查特殊字符的使用(文件名中不允许使用) 在我的旧代码中,我有这一行用于 sweetalert (1) 工作:

 var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
 if(format.test(inputValue)){   
 swal.showInputError('Special characters are not allowed');
 return false;
 }

但是当我构建这个时,它在 sweetalert2 中不起作用:

myjava <- "shinyjs.swalFromButton = function(params) { swalFromButton = function(params) {       var defaultParams = {
    title: null,
    html : null
    };
    params = shinyjs.getParams(params, defaultParams);
    swal({title : params.title, html : params.html,  
    input: 'text',
    showCancelButton : true,
    showConfirmButton : true,
    closeOnConfirm: false,
    confirmButtonColor: '#339FFF',
    allowOutsideClick: false,
  inputValidator: function(value) {
if(value === '') { return !value && 'You need to write something!'}
else {
          var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
                         if(format.test(value)){   
                           return !value && 'Special characters are not allowed'}
                       else {
                         var val2= true; 
Shiny.setInputValue('option2', value, {priority: 'event'})} 
}
}
});
};

最佳答案

正如另一篇文章中所 promise 的,这是一个没有 shinyjs 的解决方案:

library(shiny)

js <- "
Shiny.addCustomMessageHandler('sweet',
  function(message) {
    swal({
      title : message.title, 
      html : message.html, 
      input : 'text', 
      showConfirmButton : true,
      confirmButtonText : 'Confirm',
      confirmButtonColor: '#00cc00',
      showCancelButton : true,
      cancelButtonText : 'Cancel',
      cancelButtonColor : '#339fff',
      allowOutsideClick: true,
      allowEscapeKey: true,
      inputValidator: function(value) {
        if(value === '') { 
          return 'You need to write something!'
        } else {
          var format = /\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:/g;
          if(format.test(value)){   
            return 'Special characters are not allowed'
          } 
        }
      }
    })
    .then(function(result){
      if(result.dismiss === swal.DismissReason.cancel) {
        swal('failure');
      } else {
      swal('success');
        Shiny.setInputValue('option1', result.value, {priority: 'event'});
      }
    });
  }
);
"
ui <- basicPage(
  tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.min.js"),
            tags$link(rel="stylesheet", type="text/css", href = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.min.css"),
            tags$script(js)
  ),
  actionButton("messageButton", "Click me")

)

server <- function(input, output, session){
  observeEvent(input$messageButton, {
    session$sendCustomMessage(type = "sweet",
                              message = list(title = paste('<span style ="color:#339FFF;">An alert with an input text'),
                                             html = "Enter text"))
  })

  observe({print(input$option1)})
}

shinyApp(ui, server)

关于javascript - sweetalert2 文本输入,并在 R Shiny 中使用 javascript 验证文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53618418/

相关文章:

r - 当您具有colnames的字符向量时,如何不使用select()dplyr选择列?

css - 使用 renderPlot() 删除 R Shiny ggplot 中 geom_sf() 对象周围的白边

r - 在按下操作按钮之前加载微调器

r - 如何在 R Shiny 中显示带有输入的简单数据框

r - 如何在 Shiny 仪表板中以特定时间间隔将新行绑定(bind)到数据表?

javascript - 编辑 html/js 代码并在网页中显示输出

javascript - 如何使用 JQuery 和 Codeigniter/PHP 刷新 3 个数据库驱动的 div?

javascript - 删除前如何在 toastr 中要求确认

javascript - 设置属性后,jquery 不在后续单击时读取数据属性

r - cox 模型不支持 "mright"生存数据