Como puedo hacer una Nueva consulta mandando unos parametros mediante boton y se recargue la tabla?

Como puedo hacer una Nueva consulta mandando unos parametros mediante boton y se recargue la tabla?

HectorRoBandera96HectorRoBandera96 Posts: 1Questions: 1Answers: 0

Hola, soy nuevo en este tema de datatables, estoy buscando ayuda para solucionar mi problema.

Tengo unos filtros de Mes y Año en mi dashboard, y hacer la busqueda (consulta) con dichos parametros y mandarlos a ajax para que se haga la consulta y haga la tabla.

//Inicio de JS
$(document).ready (function() {
    $('#visitasclientes thead th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" class="form-control" placeholder="Buscar '+title+'" />' );
        } );
    


    $.ajax({
        url: "extras/tabla_clientes/controlador_cambio_tabla.php",
        success : function(data) {
            var o = JSON.parse(data);//A la variable le asigno el json decodificado
            console.log(o);
            var tabla_clientes=$('#visitasclientes').DataTable( {
                data : o,
                
                columns: [
                    {"data": "CLIENTE"},
                    {"data": "NOMBRE_CLIENTE"},
                    {"data": "TVIP"},
                    {"data": "VISITAS"},
                    {"data": "MES"},             
                ],
                language: {
                    "lengthMenu": "Mostrar _MENU_ registros",
                    "zeroRecords": "No se encontraron resultados",
                    "info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                    "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                    "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                    "sSearch": "Buscar:",
                    "oPaginate": {
                        "sFirst": "Primero",
                        "sLast":"Último",
                        "sNext":"Siguiente",
                        "sPrevious": "Anterior"
                                },
               "sProcessing":"Procesando...",
                },  
                initComplete: function () {
                    // Apply the search
                    this.api().columns().every( function () {
                        var that = this;
         
                        $( 'input', this.header() ).on( 'keyup change clear', function () {
                            if ( that.search() !== this.value ) {
                                that
                                    .search( this.value )
                                    .draw();
                            }
                        } );
                        } );
                   
                    }
            });
        }       
    });

    jQuery("input[name=nombre3]").click(function(){
        console.log($("#inicio_year option:selected").val());  
        console.log($("#inicio_mes option:selected").val());  
        $.ajax({
            url: "extras/tabla_clientes/controlador_cambio_tabla_mes.php",
            type: "POST",
            // datatype:"json",    
            data:  {inicio_mes:$("#inicio_mes option:selected").val(),inicio_year:$("#inicio_year option:selected").val()},    
            success: function(data) {
                var cambio = JSON.parse(data);
                // console.log(inicio_mes);
                // console.log(inicio_year);
                
                console.log("Entrando a cambios");
                console.log(cambio);
                tabla_clientes.ajax.reload(null, false);
                // $('#visitasclientes').DataTable().ajax.reload(null, false); 
                //  tabla_clientes.draw();
             }
          });       
     });
});


Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    You're creating the table from the Ajax response, so there are two ways you can go.

    1. Use ajax with DataTables to query the server, using ajax.data to send back additional fields for the filter
    2. Use what you have, but either destroy() or destroy to destroy the table after the subsequent ajax replies, and recreate it again

    Colin

This discussion has been closed.