Delete records without updating page

Asked

Viewed 805 times

0

I’m trying to delete records without having to refresh the page, but I’m not succeeding, the records erase but the table only updates if I give an F5, I’m trying to do this via ajax but it’s not rolling, can someone give me a light? Here I have the delete registration button and I pass the id

<li>
   <a onclick="excluirLancamento(<?= $user->id ?>)"><i class="fa fa-trash" aria-hidden="true"></i> Excluir</a>

And here the javascript

<script type="text/javascript">
function excluirLancamento(id) {
    if (confirm('Tem certeza que deseja excluir este registro?')) {
        $.ajax({async:true, type:'post', 
            complete:function(request, json) {
                $('#caixa').html(request.responseText); 
                excluirTr('registro'+id);
            }, 
            url:'/users/delete/'+id
        }); 
    } else { return false; }
}

remembering that I am importing jquery correctly

<script src="/webroot/js/jquery-3.2.1.min.js"></script>
  • The problem would be in function excluirTr('registro'+id); not excluding rows in the html table?

  • It deletes the line, but I have to give an F5 to update the table, I wish it doesn’t need to be updated, I don’t know is correct the way I’m doing it

3 answers

1

Hello, if records delete, just put a function to delete the "visual" part in Success:

<script type="text/javascript">
function excluirLancamento(id) {
    if (confirm('Tem certeza que deseja excluir este registro?')) {
        $.ajax({async:true, type:'post', 
            complete:function(request, json) {
                $('#caixa').html(request.responseText); 
                excluirTr('registro'+id);

                // aqui a função que deleta visualmente.
                $('#user').remove();
            }, 
            url:'/users/delete/'+id
        }); 
    } else { return false; }
}

In your html:

  <a id="user" onclick="excluirLancamento(<?= $user->id ?>)"><i class="fa fa-trash" aria-hidden="true"></i> Excluir</a>
  • Hello Lucas, I did the way you mentioned, it deletes but I still have to refresh the page to remove the record

  • Try to put the line: ('#user'). remove(); in a separate function! It is also important to put the id together with the id in html and remove! To delete only the desired <a> !

  • Henrique, in the case of having to update the page to remove the record, it turns out that AJAX has not yet finished making the requests and therefore this need. An alternative would be to create a function just to put the ('#user'+id). remove() and call this function along with the ajax onclick!

  • It didn’t solve either, Putz I don’t know what mistake I’m making

0


You need to assign an ID to each release line. After the return of Ajax, this ID is removed.

<script type="text/javascript">
function excluirLancamento(id) {
    if (confirm('Tem certeza que deseja excluir este registro?')) {
        $.ajax({async:true, type:'post', 
            complete:function(request, json) {
                $('#caixa').html(request.responseText); 
                excluirTr('registro'+id);

                $("#linha"+id).remove(); //remove a linha do registro ou lançamento
            }, 
            url:'/users/delete/'+id
        }); 
    } else { return false; }
}

Updating:

Put an ID for each <TR>. For example:

<tr id="linha<?= $user->id ?>">

In the script you will delete the <TR> with ID: "line"+id. I modified the javascript above tb.

Fiddle: https://jsfiddle.net/pwgt51fx/

  • Hello David, I did as you mentioned, it deletes but I keep having to refresh the page to remove the record

  • @Henrique Put a sample of the whole code. The way you’re wondering is hard to help. Just so you know, one of the main advantages of using Ajax is not having to refresh the page. There must be something wrong with your code. Put it there for us to see.

  • David, it’s going to be difficult to put all the code here, I made it available for you to see this link here, https://bitbucket.org/ORique/ajax thank you in advance for your help

  • @Henrique You are using <table> table for each release to be deleted?

  • That, the records are inside a table

  • @Henrique Take a look at the update I posted.

  • @Henrique Fiddle: https://jsfiddle.net/pwgt51fx/

  • The vlw again...hahaha As soon as possible I will test but I believe there will be no mistake, anything I give a shout, thanks again

Show 3 more comments

0

You can do it using a iframe (Hidden) which updates when you delete the record ... and make it refresh without having to refresh the page completely.

Browser other questions tagged

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