Created
May 27, 2016 07:09
-
-
Save crystrk/32f1fb5d32102537e534b75d443ae297 to your computer and use it in GitHub Desktop.
SweetAlert2 + Laravel | confirmation delete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var deleter = { | |
linkSelector : "a[data-delete]", | |
modalTitle : "Are you sure?", | |
modalMessage : "You will not be able to recover this entry?", | |
modalConfirmButtonText: "Yes, delete it!", | |
laravelToken : null, | |
url : "/", | |
init: function() { | |
$(this.linkSelector).on('click', {self:this}, this.handleClick); | |
}, | |
handleClick: function(event) { | |
event.preventDefault(); | |
var self = event.data.self; | |
var link = $(this); | |
self.modalTitle = link.data('title') || self.modalTitle; | |
self.modalMessage = link.data('message') || self.modalMessage; | |
self.modalConfirmButtonText = link.data('button-text') || self.modalConfirmButtonText; | |
self.url = link.attr('href'); | |
self.laravelToken = $("meta[name=token]").attr('content'); | |
self.confirmDelete(); | |
}, | |
confirmDelete: function() { | |
swal({ | |
title : this.modalTitle, | |
text : this.modalMessage, | |
type : "warning", | |
showCancelButton : true, | |
confirmButtonColor: "#DD6B55", | |
confirmButtonText : this.modalConfirmButtonText, | |
closeOnConfirm : true | |
}).then(function(isConfirm) { | |
if (isConfirm) { | |
this.makeDeleteRequest(); | |
} | |
}.bind(this)); | |
}, | |
makeDeleteRequest: function() { | |
var form = | |
$('<form>', { | |
'method': 'POST', | |
'action': this.url | |
}); | |
var token = | |
$('<input>', { | |
'type': 'hidden', | |
'name': '_token', | |
'value': this.laravelToken | |
}); | |
var hiddenInput = | |
$('<input>', { | |
'name': '_method', | |
'type': 'hidden', | |
'value': 'DELETE' | |
}); | |
return form.append(token, hiddenInput).appendTo('body').submit(); | |
} | |
}; | |
deleter.init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta name="token" content="{{csrf_token()}}"> | |
<a class="button-pointed-small" | |
data-delete="" | |
data-title="Hapus Data Ini?" | |
data-message="{{ $row->judul }}" | |
data-button-text="Hapus" | |
href="{{ route('dashboard.materi.destroy',$row->id) }}" | |
><span>Hapus</span></a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment