Problems when reloading Datatable

0

Greetings to all, I have this problem with a Datatable table which in my .JS file I declare at the beginning of this way:

var table;
$(document).ready(function(){
     table = $('#example').DataTable({
        data: "",
        'rowsGroup': [0,1,3],
        'scrollY': "200px",
        'scrollCollapse': true,
        'paging': false,
        'scrollX': true,
        language: {
            url: 'js/Spanish.json'
        }
    });
});

The point is that then, within a function which will be occupied repeatedly I want to empty this table, load it with an array called myData2 and then reload the table, but I do not know how to make this work ... the commented code inside the result of the function is the one that before used to load it of a single time and it works correctly, but I do not know how to do it for when I want to reload the data of the table of the same array with different data:

   function cargarTabla(am){
    $("#cargando").dialog("open");
    $.ajax({
        type: "GET", 
        url: "PruebaController.php",
        data: { accion: 1, am: am }
    }).done(function(data) {
        try {
            var obj = $.parseJSON(data);
            if (obj.error == 0) {
                myData2 = [];
                $.each(obj.arreglo, function(value) { myData2.push(this)});
                $(document).ready(function(){


                    // ESTAS 3 LINEAS DE CÓDIGO SON LAS QUE QUIERO CORREGIR Y HACER FUNCIONAR //

                    table.clear();
                    table.rows.add(myData2);
                    table.ajax.reload();
                    // var table = $('#example').DataTable({
                    //  data: myData2,
                    //  'rowsGroup': [0,1,3],
                    //  'scrollY': "200px",
                 //        'scrollCollapse': true,
                 //        'paging': false,
                 //        'scrollX': true,
                 //        language: {
                    //         url: 'js/Spanish.json'
                    //     }
                    // });
                });
                $("#cargando").dialog("close");
            } else {
                $("#cargando").dialog("close");
                mostrarError(obj.msg);
            }
        } catch(err) {
            console.log(err);
            $("#cargando").dialog("close");
            mostrarError(msg.err1);
        }
    }).fail(function(jqXHR, textStatus) {
        $("#cargando").dialog("close");
        if (jqXHR.status === 0) mostrarError(msg.err0);
        else if (textStatus === 'timeout') mostrarError(msg.err99);
        else mostrarError(msg.err1);
    });
}
    
asked by Roldan 26.09.2017 в 23:45
source

1 answer

0

Answering my question ... I managed to make it work this way

table.clear (); $ .each (obj.arreglo, function (value) {table.row.add (this) .draw ()});

    
answered by 27.09.2017 в 15:44