Reputation: 7128
I'm trying delete my data after confirmation and for that I want to use sweetalert.
If I use simple alert like:
deletePage(index) {
if (confirm("Do you really want to delete it?")) {
let page = this.pages[index];
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
}
},
Works fine
When I want use sweetalert like:
deletePage(index) {
let page = this.pages[index];
swal({
title: "Are you sure ?",
text: "You will not be able to recover this page !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it !",
cancelButtonText: "No, cancel !",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
}
else {
swal("Cancelled", "Your page has not been deleted !", "error");
}
}
);
}
Doesn't work!
Error I get is:
uncaught exception: SweetAlert: Unexpected 2nd argument (function (isConfirm) {
var _this2 = this;
if (isConfirm) {
axios.delete('/api/pagedelete/' + page.id).then(function (response) {
_this2.pages.splice(index, 1);
});
} else {
swal("Cancelled", "Your page has not been deleted !", "error");
}
})
Any idea how to fix this?
Upvotes: 2
Views: 3635
Reputation: 18187
The documentation shows it using promises for confirmation:
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
} else {
swal("Your imaginary file is safe!");
}
});
Your example would then be:
deletePage(index) {
let page = this.pages[index];
swal({
title: "Are you sure ?",
text: "You will not be able to recover this page !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it !",
cancelButtonText: "No, cancel !",
closeOnConfirm: false,
closeOnCancel: false
}).then((confirmed) => {
if (confirmed) {
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
} else {
swal("Cancelled", "Your page has not been deleted !", "error");
}
});
}
Upvotes: 2