甜蜜警报删除确认拉拉维尔



我在laravel中遇到问题,无法通过sweetalert确认帖子删除

<script>
!function ($) {
"use strict";
var SweetAlert = function () {
};
//examples 
SweetAlert.prototype.init = function () {
$('.sa-remove').click(function () {
swal({
title: "are u sure?",
text: "lorem lorem lorem",
type: "error",
showCancelButton: true,
confirmButtonClass: 'btn-danger waves-effect waves-light',
confirmButtonText: "Delete",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true
},
function(){
window.location.href = "{{ route('panel.posts.remove',$post->id) }}";
});
});
},
//init
$.SweetAlert = new SweetAlert, $.SweetAlert.Constructor = SweetAlert
}(window.jQuery),
//initializing 
function ($) {
"use strict";
$.SweetAlert.init()
}(window.jQuery);
</script>    

但是我在视图中有一个foreach,它只是通过最后一个foreach帖子id,当我想删除例如表中的第二篇文章时,最后一个帖子会删除!

下表如下:

<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Author</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->body }}</td>
<td>{{ $post->user->name }}</td>
<td>
<a href="#" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a>
</td> 
</tr>
@endforeach
</tbody>

我当然是新手!

您删除了错误的模态对象。首先,您应该向链接按钮添加数据属性

<a href="#" data-id="{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a> code here

然后在 javascript 代码中检索属性值并更改 url。

$('.sa-remove').click(function () {
var postId = $(this).data('id'); 
swal({
title: "are u sure?",
text: "lorem lorem lorem",
type: "error",
showCancelButton: true,
confirmButtonClass: 'btn-danger waves-effect waves-light',
confirmButtonText: "Delete",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true
},
function(){
window.location.href = "your-url/" + postId;
}); here

问题出在你的 url 上,你没有动态地选择需要删除的模型的 id,那是因为你在 url 上的 javascript 中你刚刚打印了 $post->id,这是你不应该做的事情,因为像这样混合 php 和 js 是不好的做法......

因此,为了解决您的问题,您应该正确设置您的 href,要么直接将其插入您的 href 属性,要么不将 php 与 js 混合,例如:使用 JS 选择器而不是 php 选择 $post->id,您正在尝试将 php 打印到 javascript 上,这没有任何意义。你在js中的php代码将运行一次,这就是为什么它打印最后一个id而不是你点击的元素......

你应该尝试做这样的事情... :

function(){
window.location.href =  // select post id with js here;
});

但是我要做的是在您的 foreach 上设置 href,以便您已经设置并准备好发布到您需要的路线,这样这更有意义

<a href="/your-delete-route/{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a>

如果您使用多个记录,则可以使用以下完全动态的代码:

<a href="{{ route('users.destroy', $entity->id) }}"
class="confirmation"
data-title="Delete User"
data-text="Are you sure want to delete this user? ">
<i class="icon-trash"></i>
</a>

从数据属性中,您可以获得甜蜜框的动态 URL 和标题。 然后将这些信息传递给Javascript。

jQuery(document).on('click', '.confirmation', function (e) {
e.preventDefault(); // Prevent the href from redirecting directly
var linkURL = $(this).attr("href");
var _title = $(this).attr("data-title");
var _text = $(this).attr("data-text");
warnBeforeRedirect(linkURL, _text, _title);
});

制作功能,然后确认并将用户重定向到删除方法:

function warnBeforeRedirect(linkURL, _text, _title) {
swal({
title: _title,
text: _text,
type: 'warning',
showCancelButton: true,
html: true,
}, function () {
var form = $('<form>', {
'method': 'POST',
'action': linkURL
});
var hiddenInput = $('<input>', {
'name': '_method',
'type': 'hidden',
'value': 'DELETE'
});
hiddenToken = $('<input>', {
'name': '_token',
'type': 'hidden',
'value': jQuery('meta[name="csrf-token"]').attr('content')
});
form.append(hiddenInput).append(hiddenToken).appendTo('body').submit();
});
}

如果您使用的是Laravel删除路由,那么您还需要将令牌传递到隐藏中。所以我创建了表单,并用带有一些隐藏变量的 Body 标签附加它。然后只需提交即可。

希望对您有所帮助。祝你好运。

最新更新