我在視圖中列出了類別。刪除類別按鈕也存在於工作中的視圖中,並在點擊時刪除類別。Laravel 5.2 - Sweet Alert確認框
我想要做的是在刪除一個類別之前,彈出一個甜蜜的提示對話框並要求確認。如果確認,應該轉到定義的路線並刪除類別。
的刪除鏈接的定義是這樣的:
<a id="delete-btn" href="{{ route('admin.categories.destroy', $category->id) }}" class="btn btn-danger">Delete</a>
和腳本的定義如下:
<script>
$(document).on('click', '#delete-btn', function(e) {
e.preventDefault();
var link = $(this);
swal({
title: "Confirm Delete",
text: "Are you sure to delete this category?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function(isConfirm){
if(isConfirm){
window.location = link.attr('href');
}
else{
swal("cancelled","Category deletion Cancelled", "error");
}
});
});
</script>
然而,當我點擊刪除按鈕,它刪除了類,但甜蜜的警報消息不會顯示出來。
路線被定義爲以下:
Route::get('/categories/destroy/{category}', [
'uses' => '[email protected]',
'as' => 'admin.categories.destroy',
]);
和控制器函數被定義爲:
public function destroy(Category $category)
{
$category->delete();
//this alert is working fine. however, the confirmation alert should appear
//before this one, which doesn't
Alert::success('Category deleted successfully', 'Success')->persistent("Close");
return redirect()->back();
}
任何幫助,將不勝感激。謝謝。
工作!非常感謝。 – byteseeker