我正在使用Sweet Alert测试一个方法,以改进使用laravel框架的Javascript警报方法发出的消息。
1 - 我下载了文件sweetalert.css和sweetalert.min.js。
2 - 所以我从app.blade.php
连接文件<!-- Sweet Alert -->
<link href="{{ asset('/dist/css/sweetalert.css') }}" rel="stylesheet">
<!-- Sweet Alert -->
<script src="{{ asset('/dist/js/sweetalert.min.js') }}"></script>
3 - 我使用Javascript的onclick事件和以下Sweet Alert功能创建了删除按钮:
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
<button onclick="
swal({
title: 'Esta seguro de realizar esta Acción?',
text: 'Si procede este usuario será eliminado!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Eliminar!',
cancelButtonText: 'Cancelar',
closeOnConfirm: false,
closeOnCancel: false
},
function(){
swal('Usuario eliminado!', 'Este usuario fue eliminado de nuestros registros.', 'success');
});"
class="btn btn-danger" data-toggle="tooltip" data-placement="top" title="Eliminar usuario"> <i class="material-icons">delete</i>
</button>
{!! Form::close() !!}
4 - 这是我从UserController中删除用户的方法:
public function destroy($id)
{
User::find($id)->delete();
return redirect()->route('users.index')
->with('success','User deleted successfully');
}
5 - 删除用户时出现问题,显示警告信息。
但是,自动关闭并删除用户,而不允许采取确认操作,无论是否删除用户,Sweet Alert中定义的方法。
可以帮助解决此问题或建议更好的方法,因为我使用Laravel作为框架。
答案 0 :(得分:3)
您正在执行按钮单击操作,无论您是否确认或取消删除。
<a href="" class="button" data-id="{{$user->id}}">Delete</a>
Jquery和Ajax:
$(document).on('click', '.button', function (e) {
e.preventDefault();
var id = $(this).data('id');
swal({
title: "Are you sure!",
type: "error",
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes!",
showCancelButton: true,
},
function() {
$.ajax({
type: "POST",
url: "{{url('/destroy')}}",
data: {id:id},
success: function (data) {
//
}
});
});
});
和
public function destroy(Request $request)
{
User::find($request->id)->delete();
return redirect()->route('users.index')
->with('success','User deleted successfully');
}
答案 1 :(得分:2)
在视图中:
<button onclick="deleteItem(this)" data-id="{{ $user->id }}">Delete</button>
在Jquery和Ajax中:
<script type="application/javascript">
function deleteItem(e){
let id = e.getAttribute('data-id');
const swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: 'btn btn-success',
cancelButton: 'btn btn-danger'
},
buttonsStyling: false
});
swalWithBootstrapButtons.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
reverseButtons: true
}).then((result) => {
if (result.value) {
if (result.isConfirmed){
$.ajax({
type:'DELETE',
url:'{{url("/user/delete")}}/' +id,
data:{
"_token": "{{ csrf_token() }}",
},
success:function(data) {
if (data.success){
swalWithBootstrapButtons.fire(
'Deleted!',
'Your file has been deleted.',
"success"
);
$("#"+id+"").remove(); // you can add name div to remove
}
}
});
}
} else if (
result.dismiss === Swal.DismissReason.cancel
) {
swalWithBootstrapButtons.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
);
}
});
}
</script>
在控制器中:
public function destroy(User $user, Request $request, $id)
{
if ($request->ajax()){
$user = User::findOrFail($id);
if ($user){
$user->delete();
return response()->json(array('success' => true));
}
}
}
在路线上
Route::delete('/user/delete/{id}','UserController@destroy');
答案 2 :(得分:0)
我已将此代码实现到我的laravel项目中,我的路线是销毁的资源路线。 这段代码对我有用。就像上面的注释中的location.reload()帮助我,我将其放入代码中……请尝试并让我知道...它是否对您有用...
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
//.deletebutton is the class name in button
<script>
$('.deletebutton').on('click', function () {
// return confirm('Are you sure want to delete?');
event.preventDefault();//this will hold the url
swal({
title: "Are you sure?",
text: "Once clicked, this will be softdeleted!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Done! category has been softdeleted!", {
icon: "success",
button: false,
});
location.reload(true);//this will release the event
} else {
swal("Your imaginary file is safe!");
}
});
});
</script>
答案 3 :(得分:0)
我已经在laravel项目中实现了此代码,并通过将路径名与slug一起使用来删除数据。该代码对我有用。而且我还通过使用id从表中删除行而无需reload()。因此,请尝试使用此代码,让我知道它是否对您有用。
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
function confirmDelete(slug) {
swal({
title: "Are you sure!",
type: "error",
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes!",
showCancelButton: true,
})
.then((willDelete) => {
if (willDelete.value == true) {
var url = '{{ route("instrument.delete", ":slug") }}';
url = url.replace(':slug', slug);
$.ajax({
url: url,
type: "POST",
data: {
'_method': 'DELETE'
},
success: function (data) {
if (data.status == 1) {
swal({
title: "Success!",
type: "success",
text: "Instrument has been deleted \n Click OK",
icon: "success",
confirmButtonClass: "btn btn-outline-info",
});
$('#tr' + data.slug).remove();
}
},
error: function (data) {
swal({
title: 'Opps...',
text: data.message,
type: 'error',
timer: '1500'
})
}
})
}
});
}
答案 4 :(得分:0)
我认为我的代码更接近Laravel框架,它具有 CSRF ,并且使用了 Delete 方法;集成起来也更容易。
UserController:
public function destroy(User $user)
{
$user->delete();
toast('Your file has been deleted !', 'success');
return redirect(route('user.index'));
}
删除html表单: 用于CSS类的引导程序
<form id="delete-user-form" action="{{route('user.destroy',$user)}}" method="POST">
@csrf
@method('DELETE')
<button onclick="return false" id="delete-user" class="btn btn-danger">Delete</button>
</form>
jQuery
$('#delete-post').on('click', function (e) {
e.preventDefault();
let id = $(this).data('id');
Swal.fire({
title: 'Are you sure ?',
text: "You won't be able to revert this !",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$('#delete-post-form').submit();
}
})
});