Use fadeOut ('slow') in Array Ajax containing several id

1

Hello,

I have a function in ajax, in which I do a multi-line removal in an html table.

The function collects several id that will be deleted, I would like to make a fadeOut('slow') when the function is executed.

Part of the Table

<tr id="<?php echo $produto['idarquivo']; ?>" >

This id is already being collected in the ajax function, I would like to use the effect in the part where the table is removed.

JS Code with the Ajax Function

(I tried to make fadeOut('slow') run, but I could not (I already tested it in the debugger and the ajax delete function is executed).

$('#btn_delete').click(function(){

    if(confirm("Deseja deletar as linhas selecionadas?"))
    {
        var id = '';

        $('.checkbox:checked').each(function(){
            id += $(this).val()+',';
        });

        if(id == '' || id == ',') 
        {
            alert("Selecione uma linha para deletar");
        }
        else
        {
            $.ajax({
             url:'deletar.php',
             type:'POST',
             data:{ids:id},
             success:function()
             {
                for(var i=0; i<id.length; i++)
                {
                    $('tr#'+id[i]+'').css('background-color', '#ccc');
                    $('tr#'+id[i]+'').fadeOut('slow');
                }
            }

        });
    }

}
else
{
    return false;
}
});

deletar.php

<?php include_once('conexao.php');

$ids = $_POST['ids'];

print_r($ids);

$exp = explode(",", $ids);

foreach($exp as $id){

    $query = "DELETE FROM arquivo WHERE idarquivo = '$ids'";
    $resultado = mysqli_query($con, $query);
}

?>
    
asked by anonymous 10.03.2017 / 17:12

1 answer

1

My suggestion is to have a string of id s like this: foo,bar,baz you have this in an array and treat everything as an array / collection ...

So:

$('#btn_delete').click(function() {
  if (confirm("Deseja deletar as linhas selecionadas?")) {
    var ids = $('.checkbox:checked').map(function() {
      return this.value;
    }).get();

    if (ids.length == 0) {
      alert("Selecione uma linha para deletar");
    } else {
      $.ajax({
        url: 'deletar.php',
        type: 'POST',
        data: {
          ids: JSON.stringify(ids)
        },
        success: function() {
          for (var i = 0; i < id.length; i++) {
            $('tr#' + id[i]).css('background-color', '#ccc').fadeOut('slow');
          }
        }
      });
    }
  } else {
    return false;
  }
});
    
10.03.2017 / 17:29