Placing delete method inside a Jquery append

Asked

Viewed 89 times

0

Guys I’m with the following situation, I make an append and inside it I need to send a delete method, to treat it in the web service I’m having doubts, I’ve tried to create a form inside, but it doesn’t work, detail if I put the link "http://localhost/projectohtml/admin/users/'+v.iduser+'/delete" inside href it works but in the webservice dealing with delete method or whatever it requires it to be this method, and if I change in the webservice to get it works, but it’s not safe, jquery code, :

<script>

 $(document).ready(function(){

    $.getJSON('http://localhost/projetohtml/admin/users-list-all',function(data){
      $.each(data, function(k, v){

        $('#table-users').append("<tr>"+'<td>'+v.iduser+'</td>'+
                                        '<td>'+v.desperson+'</td>'+
                                        '<td>'+v.desemail+'</td>'+
                                        '<td>'+v.deslogin+'</td>'+
                                        '<td>'+v.inadmin+'</td>'+
                                        '<td><a href="/projetohtml/admin/users/'+v.iduser+'"class="btn btn-primary btn-xs"><i class="fa fa-edit"></i> Editar</a></td>"'+
                                        '<form action="http://localhost/projetohtml/admin/users/'+v.iduser+'/delete" id="form-delete" method="delete">'+
                                        '<td><a href="#" id="iduser-delete" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Excluir</a></td>'+'</form>'+"</tr>");




      });

    });
 });



</script>
  • Instead of using a form, make an Ajax that will be fired in the event of clicking on a certain delete button, passing as parameter the ID of the item you want to remove.

  • i tried to do with ajax, but it seems that after the append in the id of <a href> ajax does not recognize the element, I do not know why, but I did the same code with ajax and did not give.

  • This happens because the element was not loaded in the DOM, but this is simple to solve. I will put as an answer to help you.

  • Beauty’s waiting for me, thanks

1 answer

2


You can do this using Ajax, which will be triggered in a delete button click event, and in it you will get the ID of the item you want to remove as parameter.

<script>
     $(document).ready(function(){
         $.getJSON('http://localhost/projetohtml/admin/users-list-all',function(data){
             $.each(data, function(k, v){
                $('#table-users').append("<tr>"+'<td>'+v.iduser+'</td>'+
                                    '<td>'+v.desperson+'</td>'+
                                    '<td>'+v.desemail+'</td>'+
                                    '<td>'+v.deslogin+'</td>'+
                                    '<td>'+v.inadmin+'</td>'+
                                    '<td><a href="/projetohtml/admin/users/'+v.iduser+'"class="btn btn-primary btn-xs"><i class="fa fa-edit"></i> Editar</a></td>"'+
                                    '<td><a href="javascript:;" id="'+ v.iduser +'" class="btn btn-danger btn-xs delete-user"><i class="fa fa-trash"></i> Excluir</a></td>'+"</tr>");
                });
          });
     });
</script>

Since your element was not loaded directly into the DOM you will need to call the event a little different click. Note that first you must pass an element that was loaded in the DOM, then you will use the "on" in the "click" event in the element that was added dynamically.

$('body').on('click', '.delete-user', function () {
    var id_user = $(this).attr('id');

    $.ajax({
        url: 'url',
        method: 'POST',
        data: { id_user: id_user },
        success: function ( response ) {
           console.log(response);
        },
        error: function () {

        }
    });
});
  • The boy the request was, however, getting an error but it’s because the URL is wrong, it’s coming : http://localhost/projectohtml/admin/href=%22/projectohtml/admin/users/43/delete%22 , I put it together like this : url: 'href="/projectohtml/admin/users/'+id_user+'/delete"' will be what’s wrong ?

  • I got ! tidy up, man thanks a lot worked super well, you would have some gift doc to get me ready to give a study ? Thanks

  • Good that it worked! Friend I don’t have but you find it easy on google.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.