How to set initial pagesize, pageindex, etc.. when using sAjaxSource

How to set initial pagesize, pageindex, etc.. when using sAjaxSource

mungletmunglet Posts: 14Questions: 0Answers: 0
edited May 2009 in General
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.

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    edited May 2009
    Hi munglet,

    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
  • mungletmunglet Posts: 14Questions: 0Answers: 0
    edited May 2009
    When would I do the parsing of the URL anchor hash? Would I then set the cookie information directly for "SpryMedia_DataTables_" ? When do I set the information so that the it is set prior to the initial ajax callback?

    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");
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    1. The parsing of the URL anchor hash would be done at page load time. So the user loads the page, the url is parsed and then DataTables is initialised with those values.

    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.
  • mungletmunglet Posts: 14Questions: 0Answers: 0
    edited May 2009
    Ok, I can see parsing the URL and load time, what I don't understand is the sytnax for initi the datatable with these extra settings. I don't see them available in the documenation or examples. For example, i see that iDisplayLength is available as a parameter, but iDisplayStart is not.

    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.
  • mungletmunglet Posts: 14Questions: 0Answers: 0
    edited May 2009
    Also,

    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?
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    And bSaveState is set to true? I'm honestly not sure what might be happening there - it should certainly take the values that come from the "cookie" if you have state saving enabled.

    Do you have a link you could post with a running example of this code?

    Thanks,
    Allan
This discussion has been closed.