Невозможно отменить удаление в подсластителе, используя Laravel 5.7. - PullRequest
0 голосов
/ 13 января 2020

Я использую Chameleon SweetAlert, моя проблема в том, что когда я нажимаю кнопку отмены, она также удаляет строку

Вот мой JS код:

 $('#confirm-dialog').on('click', function (e) {
    var that = $(this)

    e.preventDefault();

    swal({
        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(function () {
        that.closest('form').submit();
        swal('Deleted!', 'Your file has been deleted!', 'success')
      }).catch(swal.noop)
});

это код лезвия:

    @if (auth()->user()->hasPermission('delete_agents'))
                                                <form action="{{ route('dashboard.agents.destroy', $agent->id) }}" method="post" style="display: inline-block">
                                                    {{ csrf_field() }}
                                                    {{ method_field('delete') }}
                                                    <button type="button" class="btn btn-outline-danger  btn-min-width box-shadow-5 mr-1 mb-1 " id="confirm-dialog"><i class="fa fa-trash"></i> @lang('site.delete')</button>
                                                </form><!-- end of form -->
    @else
                                            <button class="btn btn-outline-danger btn-min-width box-shadow-5 mr-1 mb-1 disabled"><i class="fa fa-trash"></i> @lang('site.delete')</button>
@endif

и вот код контроллера:

 public function destroy(Agent $agent)
{
    $agent->delete();
    session()->flash('success', __('site.deleted_successfully'));
    return redirect()->route('dashboard.agents.index');
}

Ответы [ 2 ]

0 голосов
/ 15 января 2020
 <button class="btn btn-danger waves-effect" type="button" onclick="deletecategory({{ $category->id }})"> <i class="material-icons">{{__('delete')}}</i>
   </button>
  <form id="delete-form-{{ $category->id }}" action="{{ 
  route('admin.category.destroy',$category->id) }}" method="POST" style="display: none;">
     @csrf
     @method('DELETE')
   </form>

<script type="text/javascript">
    function deletecategory(id) {
        swal({
            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!',
            cancelButtonText: 'No, cancel!',
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            buttonsStyling: false,
            reverseButtons: true
        }).then((result) => {
            if (result.value) {
                event.preventDefault();
                document.getElementById('delete-form-'+id).submit();
            } else if (
                // Read more about handling dismissals
                result.dismiss === swal.DismissReason.cancel
            ) {
                swal(
                    'Cancelled',
                    'Your data is safe :)',
                    'error'
                )
            }
        })
    }
</script>
0 голосов
/ 13 января 2020

Спасибо всем

я исправил это

 $('#confirm-dialog').on('click', function (e) {
    var that = $(this)

    e.preventDefault();
    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.value) {
            that.closest('form').submit();
            Swal.fire(

                'Deleted!',
                'Your file has been deleted.',
                'success'
            )
        }
    })



});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...