Return to same page after crud of a modal

Asked

Viewed 182 times

0

I have a page with a datatables, in the event select of this table open a modal passing the id of the selected record to modal, in the modal I delete this record, soon after I close the modal and redirect to the page that was previously, but the table is empty, that is, did not update with the new records.

Table: Javascript

$('#TabEvolucao').DataTable({
        "processing" : true, 
        "select": true      ,
        "ajax" : {
            "type" : "POST",
            "url" : "estrutura/tabevolucao.php",
            dataSrc : '',
            "data" : function(d){
                d.idcliente = vlsidcliente;
                d.acao = "select";
                }
        },
        "columns" : [   {"data" : "id"}, 
                        {"data" : "data"}, 
                        {"data" : "descricao"},
                        {"data" : "nome"},
                        {"data" : "ativo"}
                    ],      
        "aaSorting": [[1,'asc']],
         "iDisplayLength": 7,
         "bFilter": true,
         "aaSorting": [[1,'asc']],              
        "language": {               
            "sEmptyTable": "Nenhum registro encontrado",
            "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
            "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
            "sInfoFiltered": "(Filtrados de _MAX_ registros)",
            "sInfoPostFix": "",
            "sInfoThousands": ".",
            "sLengthMenu": "_MENU_ resultados por página ",
            "sLoadingRecords": "Carregando...",
            "sProcessing": "Processando...",
            "sZeroRecords": "Nenhum registro encontrado",
            "sSearch": "Pesquisar",
            "oPaginate": {
                "sNext": "Próximo",
                "sPrevious": "Anterior",
                "sFirst": "Primeiro",
                "sLast": "Último"
                },
            "oAria": {
                "sSortAscending": ": Ordenar colunas de forma ascendente",
                "sSortDescending": ": Ordenar colunas de forma descendente"
                }
            }   

    });

Sending data from the selected registry to modal: Javascript

var table = $('#TabEvolucao').DataTable();
    table
        .on( 'select', function ( e, dt, type, indexes ) {
            var rowData = table.rows( indexes ).data().toArray();
            //events.prepend( '<div><b>'+type+' selection</b> - '+JSON.stringify( rowData )+'</div>' );
            //var dt = JSON.stringify( rowData );

            var id = rowData["0"].id; 

            document.getElementById('idevolucao').value = id;

            $('#modalVWEVOLUCAO').data('id', id).modal('show');


        } )

Delete button in MODAL Javascript

$('#btnUpdateYesEVO').click(function () {
        var id = $('#myModalUpdateEVO').data('id');         
        var idevolucao = document.getElementById('idevolucao').value;
        var descricaoevolucao = document.getElementById('descricaoevolucao').value;
        var boatao = document.getElementById('acaobotaoevolucao').value;
        var iduser=$('#iduser').val();

        if(boatao == 'DESATIVAR'){

            $.post('estrutura/excluirevolucao.php',{
                    acao:'delete',
                    id:id,
                    user_id:iduser                  
                    },function(r) { 
                       var m = jQuery.parseJSON(r);        
                       if (m.success) {

                        $('#myModalUpdateEVO').modal('hide');
                        toastr["success"](m.msg);             
                        $("#listevolucao").load(location.href + " #listevolucao>", "");//<<<<<< esta aqui o problema    

                       } else {            
                        toastr["error"](m.msg);
                        $('#myModalUpdateEVO').modal('hide');

                       }             
                    });
        }
  • Confusing Java with Javascript is the same thing as confusing a dog with a hot dog. Your question is about Javascript, not Java.

  • All right Victor, can you help with that question?

  • I discovered, just update the table.. $('#Tabevolucao'). Datatable().ajax.Reload();

  • Then post your solution as an answer.

1 answer

0


follows solution:

$('#TabEvolucao').DataTable().ajax.reload();

How did my code turn out:

$('#btnaddEVO').click(function () {
        var id = $('#modalADDEVO').data('id');          
        var adddataevolucao = $('#adddataevolucao').val();
        var adddescricaoevolucao =$('#adddescricaoevolucao').val();     
        var iduser=$('#iduser').val();

        $.post('estrutura/adicionaevolucao.php',{
                    acao:'insert',
                    id:id,
                    user_id:iduser,     
                    data:adddataevolucao,       
                    descricao:adddescricaoevolucao
                    },function(r) { 
                       var m = jQuery.parseJSON(r);        
                       if (m.success) {

                        $('#modalADDEVO').modal('hide');
                        toastr["success"](m.msg);             
                        $('#TabEvolucao').DataTable().ajax.reload();


                       } else {            
                        toastr["error"](m.msg);
                        $('#modalADDEVO').modal('hide');

                       }             
                    });

    });
  • Just remember that if you initialize Datatable again it recreates the list and this may lose some property that you have put, for example, sorting on startup.

Browser other questions tagged

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