I finally got my datatable to reload on intervals. But what I'm noticing now is if you scrolled to the right, the table reload will reset the scroll back to the left. The same if you are scrolled down, it will reset back to the top.
I need to stop this from happening.
I've tried all the answers from this post: How to maintain jQuery DataTables scroll position after .draw()
Nothing seems to work. Maybe I'm missing something.
I noticed on this post, the question is using a datatables.scroller.min.js: DataTables save state scroll position
This led me to this: https://cdn.datatables.net/scroller/1.4.2/
But there are so many CSS files there. I'm not sure which one to use, if any should be used at all.
Maybe I do not need any of the above, and I just need to adjust my code below to prevent the scroll reset from happening:
Click event:
$('.searchSubmit').on('click', function() {
updateDataTable();
var idle = 0;
var idleInterval = setInterval(timer, 5000);
$(this).mousemove(function(e){idle = 0;});
$(this).keypress(function(e){idle = 0;});
function timer()
{
idle = idle + 1;
if(idle > 2)
{
updateDataTable();
console.log('table reloaded');
}
}
});
Datatable:
function updateDataTable() {
$.ajax({
url: 'api/railmbs.php',
type: 'POST',
data: data,
dataType: 'html',
success: function(data, textStatus, jqXHR)
{
var jsonObject = $.parseJSON(data);
var table = $('#example1').DataTable({
"data": jsonObject,
"columns": [
{ "data": "BILL" },
{ "data": "CONTAINER" },
// few more columns
],
"iDisplayLength": 25,
"paging": true,
"scrollY": 550,
"scrollX": true,
"bDestroy": true,
"stateSave": true,
"autoWidth": true
});
},
error: function(jqHHR, textStatus, errorThrown)
{
console.log('fail');
}
});
}