Updating pagination for polling - Legacy API
Updating pagination for polling - Legacy API
I am using the latest DataTables, however I have not updated my code to use the new API... I am still using the Hungarian notation! I don't mind if answers use the new methods and notation, but I just wanted to frame the context of the question. ;)
I have the potential on a page to have several DataTables, each of them updating every X seconds. For this reason, I basically keep track of my own array of DataTables so that I can call fnDraw on them at the polling interval.
So, on initialize I do something like this:
pollObj[0] = $('#myTable').dataTable(options);
Then every X seconds, the table gets redrawn thus:
pollObj[0].fnDraw(false)
The problem is that since the initialization parameters are cached, the DataTable is losing track of its iDisplayStart and setting it back to 0 each time. If I'm at page 5 when the polling interval comes around again, it redraws the table back at page 1.
How can I get it to remember my iDisplayStart? In other words, how can I modify the pollObj[i] after a page change (draw) so that the next time fnDraw is called on it, the new value gets used? I found the fnSettings method, but there doesn't seem to be a setter. Or is it just fnSettings()._iDisplayStart = x?
I experimented with _iDisplayStart = x, but I just sort of shoved it into the polling method rather than the Ajax success callback... maybe it's "overriding" it because of some sort of race condition I haven't sussed out yet... maybe if I do it in the success callback it'll work...?
Any pointers?
This question has an accepted answers - jump to answer
Answers
I've noticed that in fnDrawCallback, if I log the oSettings after manually selecting a new page, I get an integer > 0 (for example, 40 when I'm on page 5).
Can I just take that whole of oSettings and apply it back to the cached pollObj[i] settings somehow?
(the tricky part will be finding the 'i' at that point, but that's a different story...)
Tried this, but no luck:
"fnDrawCallback": function (oSettings) {
var storedSettings = pollObj[0].fnSettings();
storedSettings._iDisplayStart = oSettings._iDisplayStart;
console.log(storedSettings); // at _iDisplayStart I see 40
}
Also, maybe I'm focused too much on iDisplayStart.
The bottom line is: I need to redraw based on the current page. :)
In the 1.10 API you would pass
false
to thedraw()
method: http://live.datatables.net/capasulo/1/edit .For the 1.9 API you can do similar: http://live.datatables.net/capasulo/2/edit .
When you say you are refreshing the data, how is that being done? If the table is being cleared and then new data added, yes the paging will be reset. You would need to read the page number you are currently on before clearing the data and then set it again after the new data has been added.
Allan
Thanks as always for the reply, Allan!
The method I am using for the refresh is to call
table.fnDraw(false)
on it.Faheem Farhan pointed me towards this plugin: http://datatables.net/plug-ins/api/fnStandingRedraw
It seems to do the trick. It's doing what I had "in my mind" but storing the value elsewhere and then re-applying it at the right time in the execution flow, which was something that was escaping me.
In very short order I am going to update everything to 1.10, but I had to close off this bug that came across my desk without making major changes to the code base.
Thanks again!