How to set initial pagesize, pageindex, etc.. when using sAjaxSource
How to set initial pagesize, pageindex, etc.. when using sAjaxSource
When someone is viewing a report i want them to be able to bookmark the exact location including page index, page size, searchterm, etc.. so that they can come back to the exact same spot in the report.
How can i make it so that when I initialize the datatable when AND I'm using sAjaxDataSource I can preset what the pagination values are for _iDisplayStart, iDisplayEnd, iDisplayLength?
Thanks.
How can i make it so that when I initialize the datatable when AND I'm using sAjaxDataSource I can preset what the pagination values are for _iDisplayStart, iDisplayEnd, iDisplayLength?
Thanks.
This discussion has been closed.
Replies
You could just enable state saving in DataTables (bStateSave - http://datatables.net/usage#bStateSave ). With this enabled a cookie will be stored by the user's browser with the information you are looking for, and when they come back, DataTables will initialise with those settings :-)
If you want something which the user can actually save as a bookmark, then you'll need to add the information that is stored in the cookie to the URL anchor hash, such that it can be parsed when the user loads that exact url. Then when the page loads you can check the anchor hash to see if that parsing is needed.
Edit: Oops forgot to say that this could be done on each fnDrawCallback(). You could even just use the cookie information for this - then all the hard work is already done :-)
Allan
Is there a reason why this can't be set during data table initialization? Feature request?
Could I not just expose the value as a setting like how iDisplayLength is during the construction of the databalse?
Could I add iDispalyStart to it or am I just asking for trouble?
_fnMap(oSettings, oInit, "iDisplayLength", "_iDisplayLength");
2. I don't think you would need to actually set the cookie information yourself (although if you don't want state saving then you could read it's value and then set it to blank).
3. Just before the DataTables object is initialised.
4. Why what can't be set during the initialisation? Almost every aspect of DataTables can be configured at initialisation time.
I have tried initting the datatable like the following, but neither work. I'm I close? Is it even possible?
var oDT = $('#reportdt').dataTable({
"iDisplayStart": 40, << doesn't work
"oSettings": [{"_iDisplayStart":40}] << no good either
Also, in order to be able to set the cookie before the table is initialized I have to know the cookie name. The unique guid that is used in the cookie name for the table is stored in the oSettings object. I can't get the oSettings object without creating the datatable, which then calls the ajax callback with the default initial parameters.
In testing I have hardcoded the values in _fnLoadState in dataTable.js
function _fnLoadState(oSettings) {
if (!oSettings.oFeatures.bStateSave) {
return;
}
//var sData = _fnReadCookie("SpryMedia_DataTables_" + oSettings.sInstance);
var sData = "{\"iStart\": 40,\"iEnd\": 50,\"iLength\": 10,\"sFilter\": \"\",\"sFilterEsc\": true,\"aaSorting\": [ [2,'desc']],\"aaSearchCols\": [ ['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true],['',true]]}";
...
Then I put a breakpoint in _fnAjaxUpdateDraw(). What I noticed was that in _fnAjaxUpdateDraw the values in the oSettings object (which is passed in as a parameter) are not the same values that I had manually set in _fnLoadState()
What could be going on?
Do you have a link you could post with a running example of this code?
Thanks,
Allan