Delete records without refreshing page

0

I'm trying to delete records without having to refresh the page, however I'm not having success, 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 delete the record button and step the id

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

And here is 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'm importing jquery correctly

<script src="/webroot/js/jquery-3.2.1.min.js"></script>
    
asked by anonymous 27.07.2017 / 02:34

3 answers

0

You must assign an ID for each post line. After Ajax returns, 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; }
}

Update:

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

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

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

Fiddle: link

    
27.07.2017 / 04:32
1

Hello, if the records erase, just put a function to exclude 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>
    
27.07.2017 / 04:17
0

You can do this using an iframe (hidden) that updates when you delete the record ... and make it refresh without having to refresh the page completely.

    
27.07.2017 / 06:53