Delete method with Sweet Alert in Laravel

后端 未结 5 2092
我在风中等你
我在风中等你 2020-12-16 07:10

I\'m testing a method using Sweet Alert, to improve the messages issued by the Javascript alert method with the laravel framework.

1 - I downloaded the files sweetal

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 07:51

    I have implemented this code in my laravel project and delete data by using route name with slug. This code is working for me. And i also delete row from table without reload() by using with id. So try this code let me know it's working for you or not.

     $.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'
                            })
                        }
                    })
                }
            });
    }
    

提交回复
热议问题