Delete a table when changing input data

0

I have the following table:

which is generated immediately, but when making a change of dates with

 $("#p-fechaIni").change(function() {

          tablaP($("#p-fechaIni").val(), $("#p-fechaFin").val());

        });

Send the data and make a new JSON with new data and create another table but below it:

The question here is how can I delete the table that is created when I change the dates and create the new table with the new data, that does not put it under the other table, but only one. try to do it with the function remove() :

$("#p-fechaIni").change(function() {
   $("#tablaDatos").remove();
   tablaP($("#p-fechaIni").val(), $("#p-fechaFin").val());

}); 
  

HTML

<table id="tablaDatos" class="table table-striper table-bordered table-hover">
</table> 


function tablaP(datoFechaI, datoFechaF){
        peticionDatos(
          '<?= base_url(); ?>index.php/ejemplo/usuario/datos',
            {},
            function(){},
            function(datos){
              //$("#tablaDatos").remove();
                var strtabla = "<thead><tr><td colspan='2'>Lugar</td>";
                var strDiv = "</tr><tr><td>Usuario</td><td>Rol</td>";
                var cont=0;

                $(datos).each(function(i,e){



                    strtabla += "<td colspan='2'id='c-"+e.id+"' data-lugar='"+(e.id)+"'>"+e.nombre+"</td>";
                    cont++;

                    strDiv += "<td  id='c-"+e.id+"' data-lugar='"+(e.id)+"'>Nº A.</td><td  id='c-"+e.id+"' data-lugar='"+(e.id)+"'>Saldo</td>";


                });

                strtabla += strDiv+"</tr></thead>";
                $("#tablaDatos").append(strtabla);
                cargaNum(cont, datoFechaI, datoFechaF); // esta funcion es para crear el body de la tabla con otros datos al igual que el thead se crea con la funcion append dependiendo los datos.
        });
      }

to which I delete the table but do not create the new one ... someone who can help me?

    
asked by Soldier 21.08.2017 в 18:34
source

1 answer

2

Try changing the $("#tablaDatos").remove(); by $("#tablaDatos").html(""); what happens is that the $("#tablaDatos").remove(); eliminates the table completely and when you try to add the new one that element no longer exists so do not create it with $("#tablaDatos").html(""); what you do is change the content html internal of the table element by "" which leaves it empty and ready for the new content. I hope it helps you.

You can also use $("#tablaDatos").empty(); achieving the same result.

    
answered by 21.08.2017 / 22:21
source