[英]How to use question alert using sweetalert in laravel?
I have a problem using sweetalert in Laravel how to make a confirmation alert using Alert::question
to delete data in the table?我在 Laravel 中使用 sweetalert 时遇到问题 如何使用
Alert::question
发出确认警报以删除表中的数据? this is my code, but the alert appear after the data was deleted, how to fix this problem?这是我的代码,但是数据被删除后出现警报,如何解决这个问题?
public function delete($id)
{Alert::question('Benar Ingin Hapus data?','data tidak dapat dikembalikan')->persistent('Close');
DB::table('daftar_pelanggans')->where('id',$id)->delete();
return redirect()->back();}
this is the delete button view.blade这是删除按钮 view.blade
<td>
<a href="{{ url('pages/daftar_pelanggan/edit/'.$pelanggan->id) }}" class="btn btn-sm btn-primary">
<i class="fa fa-edit"></i></a>
<a href="{{ url('daftar_pelanggan/delete/'.$pelanggan->id) }}"><button class="btn btn-sm btn-danger" ><i class="fa fa-trash"></i></button></a>
</td>
this is the routing web.php这是路由 web.php
Route::get('daftar_pelanggan/delete/{id}', [DaftarPelangganController::class,'delete'])->name('daftar_pelanggan.delete');
You should use javascript code on the page that have the delete button/link to handle it(not PHP code).您应该在具有删除按钮/链接的页面上使用 javascript 代码来处理它(不是 PHP 代码)。
an example use onclick on you delete link一个示例在您删除链接时使用 onclick
<a href="javascript:void(0)" action="{{ url('daftar_pelanggan/delete/'.$pelanggan->id) }}" onclick="deleteData(this)"><button class="btn btn-sm btn-danger" ><i class="fa fa-trash"></i></button></a>
then add below code to the bottom of your blade/view after sweetalert js.然后在 sweetalert js 之后将以下代码添加到刀片/视图的底部。
function destroyData(link) {
swal({
icon: 'warning',
title: 'Benar Ingin Hapus data?',
text: 'data tidak dapat dikembalikan',
buttons: ["No", "Yes"],
dangerMode: true,
})
.then(isClose => {
if (isClose) {
window.location = $(link).attr('action');
} else {
swal("Delete data canceled");
}
});
}
dont forget to change href
attribute to action
or another attribute name to prevent auto redirect go to href value after click.不要忘记将
href
属性更改为action
或其他属性名称,以防止在单击后自动将 go 重定向到 href 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.