简体   繁体   English

Sweet-alert在Laravel中确认后如何删除…?

[英]How to Delete After Confirmation by Sweet-alert in Laravel…?

I have linked sweetalert plugin for my confirmation message. 我已将sweetalert插件链接为我的确认消息。 But it's not getting the confirmation command (onclick yes/confirm button). 但是它没有收到确认命令(单击“是/确认”按钮)。

This is my delete button: 这是我的删除按钮:

<a id="demoSwal" href="{{ route('setting.website_type.destroy', ['id' => $websiteType->id]) }}" class="btn btn-light btn-sm btn-delete demoSwal" data-toggle="tooltip" data-placement="top" title="Delete This Type"><i class="far fa-trash-alt"></i></a>

This is My JS Code: 这是我的JS代码:

$(document).ready(function(){
    $('.demoSwal').click(function(){
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel plx!",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function(isConfirm) {
                if (isConfirm) {
                    swal("Deleted!", "Your Data file has been deleted.", "success");
                } else {
                    swal("Cancelled", "Your Data file is safe :)", "error");
                }
            });
        });
});

This is my Controller Code: 这是我的控制器代码:

public function destroy($id)
    {
        $delete_website_type = WebsiteType::find($id);
        $delete_website_type->delete();

        return redirect()->back();

    }

Please Give me the solution... 请给我解决方案...

Give it a try 试试看

for delete button:--- 用于删除按钮:---

 <a href="" class="button" data-id="{{$websiteType->id}}"><i class="far fa-trash-alt"></i></a>

in Jquery 在jQuery中

$(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('/setting/website_type/destroy')}}",
                data: {id:id},
                success: function (data) {
                              //
                    }         
            });
    });
});

and

public function destroy(Request $request)
{
    $delete_website_type = WebsiteType::find($request->id)->delete();
    return redirect()->route('website.index')
                    ->with('success','Website deleted successfully');
}

i have updated my answer try this one. 我已经更新我的答案,尝试这个。

you have to put .then() 你必须把.then()

let id = $("#your_id").val();
swal({
    title: "Are you sure?",
    text: "You will not be able to recover this file!",
    type: "warning",
    showCancelButton: true,
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: "No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
 }).then((isConfirm) => {
        if(isConfirm) {
               //you can just use fetchAPI 
               fetch(`route_to_your_destroy_method/${id}`)
               .then(response => response.json()
               .then(result => {
                     //your result
                      if (result == 'success') {
                          //alert success
                      }else {
                           //alert fail
                      }
                }).catch(err => {console.log(err)});
        }
 });

and in your destroy method 并以您的销毁方法

$delete_website_type = WebsiteType::find($id)->delete();
 if(delete_website_type) {
    $message = 'success';
 }else {
     $message = 'fail';
 }
  return json_encode($message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM