Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My first time working with Datatables and SweetAlert2. 

I have a problem in laravel for confirming the post delete by sweetalert2.

I have the following code but the confirm modal is useless, as the request pass it and user don't even have time to decide. I have to figure it out to stop request executing and waits for user decision (Ok/Cancel).


<script>
        $(document).ready(function () {
            const userTable = $('#UserFacultyTable').DataTable({
                processing: true,
                fixedHeader: true,
                ajax: {
                    url: '/Assign/GetUserFaculty',
                    dataSrc: ''
                },
                rowId: "id",
                columns: [
                    {
                        data: 'id',
                        className: "center",
                        title: 'Actions',
                        render: function (data) {
                        return '<a href="Assign/DeleteUserInFaculty/' + data + '" class="btn btn-danger" id="del"> Remove </a>';
                        }
                                                            
                    }
                ],
                order: [2, 'asc']
            });
            $('#UserFacultyTable tbody').on('click', 'id="del"', function () {          
                      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) {
                          Swal.fire(
                            'Deleted!',
                            'Your file has been deleted.',
                            'success'
                          )
                        }
                      });
                  });
        });
    </script>


What I have tried:

I tried many ways but I am not good at Javascript
Posted
Updated 29-Mar-21 3:48am
Comments
SeanChupas 29-Mar-21 16:01pm    
A better option is to put javascript on the a tag instead of having a link.

1 solution

You need to prevent the browser from following the link.

You'll also need to navigate to the link, or perform an AJAX request, when the user confirms the deletion. Otherwise, nothing will actually be deleted.
JavaScript
$('#UserFacultyTable tbody').on('click', 'id="del"', function (e) {
    e.preventDefault();
    
    const url = this.href;
    Swal.fire({
        ...
    }).then(result => {
        if (result.isConfirmed) {
            location.assign(url);
        }
    });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900