laravel Sweetalert 删除确认laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50769717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sweetalert delete confirm laravel
提问by alitnk
I have a problem in laravelfor confirming the post delete by sweetalert
我在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>
But I have a foreach
in the view and it just passes last foreach
post id
and when I want to delete for example second post in the table, last one deletes!
但是我foreach
在视图中有一个它只是通过了最后一个foreach
帖子id
,当我想删除表格中的第二个帖子时,最后一个删除!
this is the table:
这是表:
<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>
and I'm new in this of course!
我当然是新手!
回答by skm
You are deleting the wrong modal object.First you should add a data attribute to the link button
您正在删除错误的模态对象。首先,您应该向链接按钮添加数据属性
<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
Then in your javascript code retrieve the attribute value and change the url.
然后在您的 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
回答by GaimZz
The problem is in your url, you are not dinamically selectingthe id of the model that needs to be deleted, that's because in your javascript on your url you just printed $post->id, which is something you shouldn't do as mixing php and js like that is bad practice...
问题出在您的 url 中,您没有动态地选择需要删除的模型的 id,那是因为在您的 url 上的 javascript 中,您刚刚打印了 $post->id,这是您不应该做的混合操作像这样的 php 和 js 是不好的做法......
So in order to solve your problem you should set your href properly, either insert it directly on your href attribute or by not mixing php with js ,e.g: selecting the $post->id with a JS selector not php, you are trying to print php onto the javascript dinamically which doesn't make any sense. The php code you have inside js will run ONCEthat's why it's printing the last idinstead of the element you clicked...
因此,为了解决您的问题,您应该正确设置您的 href,或者直接将其插入您的 href 属性中,或者不将 php 与 js 混合,例如:使用 JS 选择器而不是 php 选择 $post->id,您正在尝试动态地将 php 打印到 javascript 上,这没有任何意义。您在 js 中的 php 代码将运行一次,这就是为什么它打印最后一个 id而不是您单击的元素...
You should be trying to do something like... :
你应该尝试做类似...的事情:
function(){
window.location.href = // select post id with js here;
});
But what I would do is to set the href on your foreach so you already have set up and ready to post to the route you need so this would make more sense
但是我要做的是在您的 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>
回答by Gautam Patadiya
If you working with mulitple records you can use this fully dynamic code:
如果您使用多条记录,您可以使用这个完全动态的代码:
<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>
From data attributes you can get dynamic url and title for the sweetalear box. and then just pass this information into Javascript.
从数据属性中,您可以获得 Sweetalear 框的动态 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);
});
Make function to then confirm and redirect user to delete method:
使功能然后确认和重定向用户删除方法:
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();
});
}
If you are using Laravel DELETE Route then you need to pass token into hidden as well. So I created form and append it with Body tag with some hidden variables. then just submit it.
如果您使用的是 Laravel DELETE Route,那么您还需要将令牌传递到 hidden 中。所以我创建了表单并将其附加了带有一些隐藏变量的 Body 标记。然后提交它。
Hope its helpful for you. Good Luck.
希望对你有帮助。祝你好运。