I'm trying to get my table to load on intervals. I am now receiving the below error:
TypeError: g is null
The user will enter form parameters and then hit the submit button which has a click event. As follows:
$('.searchSubmit').on('click', function()
{
var data = {
searchCriteria: {
bill: $('#import_bill').val(),
ramp: $('#import_ramp').val(),
// few other parameters
}
};
$.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": "RAMP" },
// few more columns
],
"iDisplayLength": 25,
"paging": true,
"bDestroy": true,
"stateSave": true,
"autoWidth": true
});
var idle = 0;
var idelInterval = setInterval(timer, 10000);
$(this).mousemove(function(e){idle = 0;});
$(this).keypress(function(e){idle = 0;});
function timer()
{
idle = idle + 1;
if(idle > 2)
{
$('#example1').DataTable().ajax.reload(); // <--error occurs here
console.log('table reloaded');
}
}
},
error: function(jqHHR, textStatus, errorThrown)
{
console.log('fail');
}
});
});
Here's the funny part...above, where I pointed to where the error was occurring, I originally had it looking like this:
$('#example').DataTable().ajax.reload();
Notice the table name was 'example' instead of 'example1'. The table ID is indeed example1, as I indicated up near where the success function begins. When I saw the reload interval was looking at a different table ID, I changed it, which now is causing the error at the top.
I don't think I should keep the ID as 'example' because that is not the correct ID.
With that said, why am I getting the error?
How do you expect
ajax.reload()
to work? There is no AJAX in use and therefore no previous AJAX to reload. Do this instead (schematic) :Now you should be able to
table.ajax.reload()
from anywhere wheretable
is available.I've worked out a solution that seems to do the trick. I tried to keep this as simple as I could, while still incorporating jQuery and (I think) solving the issue you were having.
index.html
data1.json
data2.json
My style of coding is a little different from yours, but the same basic concepts should be in play here.
Hope it helps. :-)