我已经使用Sweetalert2
创建了此弹出窗口序列用户选择年份,等待生成报告,最后可以下载报告。
这是代码(简体)
var startYear = 2017;
$("#test").click(function(){
var _id = ....;
var listYears = {};
for (var i = parseInt(moment().format("YYYY")); i >= startYear; i--) listYears[" " + i] = i;
swal({
title: "Data export",
html : "Select a year and press the <strong>export</strong> button.",
reverseButtons : true,
showCancelButton: true,
cancelButtonText : "Cancel",
confirmButtonText: "Export",
validationMessage : "Select a year",
inputClass : "form-control", /* bootstrap 4 class */
input: "select",
inputOptions: listYears,
inputPlaceholder: "Select..",
}).then((result) => {
if (result.value) {
swal({
title: 'Wait',
text: 'Report generation in progress..',
allowOutsideClick : false,
showConfirmButton : false,
onOpen: () => {
swal.showLoading();
var dataGET = ".....&id=" + _id + "&year=" + parseInt(result.value);
var xhr = $.ajax({
type: "GET",
url: ".....php",
data : dataGET,
cache: false,
success : function(val){
var _this = this;
if(val == "OK_DOWNLOAD"){
var pathDownload = xhr.getResponseHeader(".....");
var nameDownload = xhr.getResponseHeader(".....");
swal({
type : "success",
title: 'Perfect',
html : 'Now you can download the report<br/><a class="btn btn-custom-secondary mt-3" href="......" target="_blank" id="tempBtnDownloadReport"><span class="icon-download1"></span></a>',
showConfirmButton : false,
});
$("#tempBtnDownloadReport").click(function(){
swal.close();
});
}else{
_this.error();
}
},
error : function(){
swal("Attention","Error creating report, please try again.","error");
},
complete : function(jqXHR,textStatus){
swal.hideLoading();
xhr = null;
}
});
}
});
}
});
我的问题是,当用户按下导出按钮时,尚未“选择”它的选择。我想触发类似these examples之类的错误消息(“选择年份”)。
答案 0 :(得分:0)
已解决
我使用了 preConfirm 事件。
swal({
title: "Data export",
html : "Select a year and press the <strong>export</strong> button.",
reverseButtons : true,
showCancelButton: true,
cancelButtonText : "Cancel",
confirmButtonText: "Export",
validationMessage : "Select a year",
inputClass : "form-control",
input: "select",
inputOptions: listYears,
inputPlaceholder: "Select..",
allowOutsideClick: () => !Swal.isLoading(),
preConfirm: (test) => {
if(test == "") Swal.showValidationMessage("Select a year");
}
}).then((result) => {
if (result.value) {
swal({
title: 'Wait',
text: 'Report generation in progress..',
allowOutsideClick : false,
showConfirmButton : false,
onOpen: () => {
swal.showLoading();
var dataGET = "category=download&cmd=do_excel_report&id=" + _id + "&year=" + parseInt(result.value);
var xhr = $.ajax({
type: "GET",
url: "/" + $("html").data("project") + "/home/command.php",
data : dataGET,
cache: false,
success : function(val){
var _this = this;
if(val == "OK_DOWNLOAD"){
var pathDownload = xhr.getResponseHeader("Custom-Success-Download-Path");
var nameDownload = xhr.getResponseHeader("Custom-Success-Download-Name");
swal({
type : "success",
title: 'Perfect',
html : 'Now you can download the report<br/><a class="btn btn-custom-secondary mt-3" href="/' + $("html").data("project") + "/home/command.php?category=download&cmd=download_excel_report&path=" + pathDownload + "&name=" + nameDownload + '" target="_blank" id="tempBtnDownloadReport"><span class="icon-download1"></span></a>',
showConfirmButton : false,
});
$("#tempBtnDownloadReport").click(function(){
swal.close();
});
}else{
_this.error();
}
},
error : function(){
swal("Attention","Error creating report, please try again.","error");
},
complete : function(jqXHR,textStatus){
swal.hideLoading();
xhr = null;
}
});
}
});
}
});