Reputation: 2714
i have an jquery Datatables in use and trying to reload the data via interval. I can't succeed, but is this code right?
(document).ready(function() {
$.ajax({
url: "./includes/processing.php",
method: "POST",
dataType:'json',
"contentType": "application/json; charset=utf-8",
success: function(data) {
$('#DTResTableList_1').dataTable({
data: data,
'paging': false,
'scrollY': 400,
'select': true,
columns:[
{'data' : 'col0'},
{'data' : 'col1'},
{'data' : 'col2'},
{'data' : 'col3'},
{'data' : 'col4'},
{'data' : 'col5'}
]
});
},
error: function(data) {
console.log(data);
}
});
setInterval( function () {
table.ajax.reload();
}, 1000 );
});
tried to built it separately, but this doesn't let the content load itself.
SOLVED by using this code;
$(document).ready(function() {
$('#DTResTableList_1').DataTable( {
"ajax": {
url: './includes/processing.php',
method: 'POST',
dataType:'json',
contentType: 'application/json; charset=utf-8',
dataSrc: ""},
paging: false,
scrollY: 400,
select: true,
'columns': [
{'data' : 'col0'},
{'data' : 'col1'},
{'data' : 'col2'},
{'data' : 'col3'},
{'data' : 'col4'},
{'data' : 'col5'}
]
} );
setInterval( function () {
$('#DTResTableList_1').DataTable().ajax.reload( null, false ); // user paging is not reset on reload
}, 5000 );
} );
Upvotes: 0
Views: 1004
Reputation: 58890
Use the code below instead.
I suggest calling setInterval
right after successful DataTables initialization otherwise racing condition could occur.
$(document).ready(function() {
var table = $('#DTResTableList_1').DataTable({
'ajax': {
'url': "./includes/processing.php",
'method': "POST",
'dataType': 'json',
'contentType': "application/json; charset=utf-8"
},
'initComplete': function(settings){
var api = new $.fn.dataTable.Api( settings );
setInterval( function () {
api.ajax.reload();
}, 1000);
},
'paging': false,
'scrollY': 400,
'select': true,
'columns': [
{'data' : 'col0'},
{'data' : 'col1'},
{'data' : 'col2'},
{'data' : 'col3'},
{'data' : 'col4'},
{'data' : 'col5'}
]
});
});
Upvotes: 1