Declaring Datatable Variable in JQUERY Doesn't Functioning, BASIC

Declaring Datatable Variable in JQUERY Doesn't Functioning, BASIC

facubafacuba Posts: 1Questions: 1Answers: 0
edited June 2016 in Free community support

I Have a Code like this:
var caso=0;

$(document).ready(function() {
    var d = new Date();
        $.ajax({
                type:"get",
                url:"traermov.php",
                encode:true,
                    dataType:"json",
                success:function(data){
                var table = new $('#tablamov').dataTable({
                    data: data,
                    columns: [
                        { title: "ID" },
                        { title: "Lote" },
                        { title: "Etapa" },
                        { title: "Personal" },
                        { title: "Fecha" },
                        { title: "Comienzo" },
                        { title: "Fin" },
                        { title: "Cantidad" }
                        ]
                        });
                $("#movimiento").show();
                $("#movimiento").prepend("<button onclick='hoy()'>Hoy</button>");
                $("#movimiento").prepend('<h3>MOVIMIENTOS:</h3>');
            },

         });

        table.columns().flatten().each( function ( colIdx ) {
        // Create the select list and search operation
        var select = $('<select />')
            .appendTo(
                table.column(colIdx).footer()
            )
            .on( 'change', function () {
                table
                    .column( colIdx )
                    .search( $(this).val() )
                    .draw();
            } );
     
        // Get the search data for the first column and add to the select list
        table
            .column( colIdx )
            .cache( 'search' )
            .sort()
            .unique()
            .each( function ( d ) {
                select.append( $('<option value="'+d+'">'+d+'</option>') );
            } );
    } );
        event.preventDefault();
        });

GIVES this error:

Uncaught ReferenceError: table is not defined
WHY?
thnks

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Hi Facuba

    The problem seems to be your table variable definition. You have var table inside the ajax success function which is asynchronous and only executes once the ajax request has been completed. However on line 29 you are using the table variable immediately and that's why the error is occurring.

    If you move the filtering code into the success function that should fix it for you.

    Thanks

    Tom

This discussion has been closed.