Sweetalert2消息,带有R闪亮下拉菜单

时间:2019-02-11 17:35:24

标签: javascript r shiny sweetalert2

如何构建包含下拉菜单的sweetalert2消息的R闪亮版本。 我已经在R Shiny中建立了Sweetalert消息的数量和类型,但这对我来说是一种新类型,并且我坚持使用正确的方法来从消息中的selectinput中以文本而不是数字的形式来进行选择

使它可以工作到一定程度,但是输出是列表中第n个元素的数目,而不是文本字符串。....

原始的纯JavaScript示例: select 下的example(在此消息底部发布。

我制作了一个演示应用程序,该应用程序会显示数字而不是文本,然后我尝试了此操作:(按照我根据另一个关于SO关于Sweetalerts的问题最终构建的工作解决方案

myjava <- "shinyjs.swalFromButton = function(params) { 
    var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
  return new Promise(function(resolve) {
    if (value === 'Select a batchname') {
      resolve('You need to select a batchname')
    } else {
      resolve()
    }
  })
}
})
.then(function(result){
  if(result.dismiss === swal.DismissReason.cancel) {
  } else {
    Shiny.setInputValue('SweetDropChoice', result.value, {priority: 'event'});
  }
});
};"

我认为我的问题是,我不知道如何在自己的版本中正确使用示例中的解析。

这里是进行测试的应用程序。您将需要更改目录并下载两个文件以使sweetalert2工作 此处:https://www.jsdelivr.com/package/npm/sweetalert2, 下载按钮位于标题的右侧: sweetalert2 并且所需的2个文件位于 dist 文件夹中,名为

sweetalert2.min.js&sweetalert2.min.css

setwd(PASTE LOCATION WHERE YOU SAVED THE SWEETALERT SCRIPTS)

    library(shiny)
    library(shinyjs)

    myjava <- "shinyjs.swalFromButton = function(params) { 
        var defaultParams = {
    title : null,
    html : null,
    inputOptions: null
    };
    params = shinyjs.getParams(params, defaultParams);
    swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
    input: 'select',
    inputPlaceholder: 'Select a batchname',
    showCancelButton: true})
    .then(function(result){
    if (result.value === 'Select a batchname') {
    resolve('You need to select a batchname:)')
    } else {
    var batchname = result.value
    Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});}
    });
    };"



    ui  <- fluidPage(

      actionButton(inputId = 'messagebutton', label = 'click me'),
      verbatimTextOutput('Choice', placeholder = T),
      shinyjs::useShinyjs(),
      shinyjs::extendShinyjs(text = myjava),
      tags$head(includeScript("sweetalert2.min.js"),
                includeCSS("sweetalert2.min.css")
      )
    )

    server <- function(input, output, session) { 

      values <- reactiveValues(Choice = '?',
                               Choices = rownames(mtcars)[1:6] ## dummy input to use in the sweetalert with dropdown
                               )

      observeEvent(input$messagebutton, { 
        shinyjs::js$swalFromButton( title = paste('<span style ="color:#339FFF;">An alert with a choice'),
                                    html = paste('Pick a name'), 
                                    inputOptions = values$Choices)
      })


      output$Choice <- renderPrint({input$SweetDropChoice})  ## print output of new sweetalert

    }

    shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:0)

@Stéphane奇怪的是,我现在可以使用其他版本...

myjava <- "shinyjs.swalFromButton = function(params) { 
    var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === '') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
}
});
};"

答案 1 :(得分:0)

这是我的版本。但据我所知,这与您的一样。

除了我没有包括if(result.dismiss === swal.DismissReason.cancel)(我不确定这是否正确,顺便说一句,dismiss中没有result字段)。

myjava <- "
shinyjs.swalFromButton = function(params) { 
  var defaultParams = {
    title: null,
    html: null,
    inputOptions: null
  };
  params = shinyjs.getParams(params, defaultParams);
  swal({
    title: params.title, 
    html: params.html, 
    inputOptions: params.inputOptions,
    input: 'select',
    inputPlaceholder: 'Select a batchname',
    showCancelButton: true,
    inputValidator: function(value) {
      return new Promise(function(resolve) {
        if (value !== '') {
          resolve();
        } else {
          resolve('You need to select a batch :)');
        }
      });
    }
  })
  .then(function(result){
    var batchname = params.inputOptions[result.value];
    Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
  });
};"