Laravel не может подтвердить удаление с помощью SweetAlert - PullRequest
0 голосов
/ 15 мая 2019

У меня есть эта проблема со Sweetalert - у меня есть следующий код, но модал подтверждения бесполезен, так как запрос проходит его, и у пользователя даже нет времени принимать решение.Я должен разобраться, чтобы остановить выполнение запроса и ждать решения пользователя (Ok / Cancel).

Вот код:

<a href="{{route('notes.destroy', $note->id)}}" 
data-id="{{$note->id}}" onclick="confirmDelete('{{$note->id}}')" type="submit">
<span class="badge badge-danger">Delete</span></a>

, а вот jQuery (который, я думаю, этопроблема):

<script>
function confirmDelete(item_id) {
    swal({ 
        title: "Are you sure?",
        text: "Once deleted, you will not be able to recover it!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
        if (willDelete) {
            $('#' + item_id).submit();
        } else {
            swal("Cancelled Successfully");
        }
    });
}
</script>

Ответы [ 3 ]

1 голос
/ 18 мая 2019

В блейд-файле:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.js"></script>

<form id="delete_from_{{$note->id}}" method="POST" action="{{ route('post.destroy', $note->id) }}">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}

    <div class="form-group">
        <a href="javascript:void(0);" data-id="{{$note->id}}" class="_delete_data">
            <span class="badge badge-danger">Delete</span>
        </a>                    
    </div>
</form>

JS код:

<script>
    $(document).ready(function(){
        $('._delete_data').click(function(e){
            var data_id = $(this).attr('data-id');
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {

                if (result.value) {
                    $(document).find('#delete_from_'+data_id).submit();
                }
            })
        });
    });            
</script>
0 голосов
/ 15 мая 2019

Запретить отправку формы с помощью preventDefault()

function confirmDelete(event,item_id) {
event.preventDefault();
    swal({ 
        title: "Are you sure?",
        text: "Once deleted, you will not be able to recover it!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
        if (willDelete) {
            $('#' + item_id).submit();
        } else {
            swal("Cancelled Successfully");
        }
    });
}
<a href="{{route('notes.destroy', $note->id)}}" 
data-id="{{$note->id}}" onclick="confirmDelete(event,'{{$note->id}}')" type="submit">
<span class="badge badge-danger">Delete</span></a>
and here's the jQuery (which I think it's the problem):
0 голосов
/ 15 мая 2019

Если я правильно понимаю ваш вопрос, вы имеете в виду, что модальный подсластитель не предоставил вам никакой возможности продолжить или отменить решение после события onclick. попробуйте добавить «showCancelButton» и «verifyButtonText», как показано ниже, и посмотрите, имеет ли это какое-либо значение.

swal({
    title: "Are you sure?",
    text: "Once deleted, you will not be able to recover it!",
    icon: "warning",
    showCancelButton: true,
    confirmButtonText: 'ok'
}).then((willDelete) => {
    if (willDelete) {
        $('#' + item_id).submit();
    } else {
        swal("Cancelled Successfully");
    }
});
...