Confirm shipment Sweet Alert Laravel

0

I am trying to delete a record with double confirmation of sweet alert from a datatables, but when selecting one, it always sends me the id 1, no matter what the record is with the id 10, 20 or 30. What should be done? or what am I doing wrong ?. Annex code that generates the list

@foreach ($array as $row)
    <tr>
        <td>
            <img src="{{ asset('uploads/users/'.$row->login.'/image.png') }}" style="max-width:30px;">
            {{ $row->firstName.' '.$row->lastName }}
        </td>
        <td>
            {{ $row->email }}
        </td>
        <td>
            {{ $row->login }}
        </td>
        <td>

            <!-- Action buttons -->
            <div>
                {!! Form::open(array('route' => array($options['route'].'.destroy', $row->id), 'method' => 'DELETE', 'id' => 'myform'.$row->id)) !!}


                    <a href="{{ route($options['route'].'.show', $row->id) }}" class="btn btn-warning btn-sm"> 
                        <i class="la la-small la-edit"></i> 
                    </a>

                    <button type="button" id="delete" data-id="<?php echo $row->id; ?>" class="btn btn-danger btn-sm"><i class="la la-small la-trash"></i></button>

                {!! Form::close() !!}
            </div>
        </td>
    </tr>
@endforeach

and the js function that the Sweet alert generates

$('button#delete').on('click', function() {
        var id = $(this).attr('data-id');
        swal({
          title: "¿Desea eliminar el usuario?",
          text: "",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: "btn-danger",
          confirmButtonText: "Si!!",
          cancelButtonText: "No!!",
          closeOnConfirm: false,
          closeOnCancel: false
        },
        function(isConfirm) {
          if (isConfirm) {

            swal({
                title:'¡El usuario será eliminado!',
                text: '',
                type: 'success'
            }, 
            function() {
              $("#myform"+id).submit();
            });

          } else {

            swal("Cancelled", "El usuario no será eliminado!!", "error");

          }
        });

    })
    
asked by Erain Moya 23.06.2018 в 12:07
source

1 answer

1

Your problem is that you are repeating the same id and that should not be done, remember that the id should be unique, work with classes for that, .delete would be more useful to you

Change this

<button type="button" id="delete" class="btn btn-danger btn-sm"><i class="la la-small la-trash"></i></button>

for this

<button type="button" class="btn btn-danger btn-sm delete"><i class="la la-small la-trash"></i></button>


$('.delete').on('click', function() {

        swal({
          title: "¿Desea eliminar el usuario?",
          text: "",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: "btn-danger",
          confirmButtonText: "Si!!",
          cancelButtonText: "No!!",
          closeOnConfirm: false,
          closeOnCancel: false
        },
        function(isConfirm) {
          if (isConfirm) {

            swal({
                title:'¡El usuario será eliminado!',
                text: '',
                type: 'success'
            }, 
            function() {
              $("#myform").submit();
            });

          } else {

            swal("Cancelled", "El usuario no será eliminado!!", "error");

          }
        });

    })

Remember that you can also use the data attributes to add, for example, the id to identify the button where you clicked, I say this because you try to identify the form you are going to send.

Change this also leave it like this:

{!! Form::open(array('route' => array($options['route'].'.destroy', $row->id), 'method' => 'DELETE', 'id' => 'myform'.$row->id)) !!}

to your button put this data attribute:

<button type="button" data-id="<?php echo $row->id; ?>" class="btn btn-danger btn-sm"><i class="la la-small la-trash"></i></button>

and your switalert like this:

$('.delete').click(function() {
var id = $(this).attr('data-id');

        swal({
          title: "¿Desea eliminar el usuario?",
          text: "",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: "btn-danger",
          confirmButtonText: "Si!!",
          cancelButtonText: "No!!",
          closeOnConfirm: false,
          closeOnCancel: false
        },
        function(isConfirm) {
          if (isConfirm) {

            swal({
                title:'¡El usuario será eliminado!',
                text: '',
                type: 'success'
            }, 
            function() {
              $("#myform"+id).submit();
            });

          } else {

            swal("Cancelled", "El usuario no será eliminado!!", "error");

          }
        });

    })

in that way identifies which form to send when clicking on a button

    
answered by 23.06.2018 / 14:05
source