Declaring Datatable Variable in JQUERY Doesn't Functioning, BASIC
Declaring Datatable Variable in JQUERY Doesn't Functioning, BASIC
facuba
Posts: 1Questions: 1Answers: 0
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
This discussion has been closed.
Answers
Hi Facuba
The problem seems to be your table variable definition. You have
var tableinside the ajax success function which is asynchronous and only executes once the ajax request has been completed. However on line 29 you are using thetablevariable 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