iDisplayStart with sorting

iDisplayStart with sorting

greattall1greattall1 Posts: 7Questions: 0Answers: 0
edited January 2010 in General
Hi,

I am doing server-side processing for pagination and sorting on my datagrid. I just enabled the sorting functions on my dataTable (bSort, aaSorting, bSortable, etc.) and I am noticing that iDisplayStart is always 0 when the table is initialized.

I am retaining the values of iDisplayStart and iDisplayLength in cookies so that I can recall them when the user does a browser back. Is there a way I can keep iDisplayStart from being reset to 0 when the table is initialized? All of my pagination and sorting is done server-side so there should be no reason on the UI side for these values to be reset.

Replies

  • greattall1greattall1 Posts: 7Questions: 0Answers: 0
    I just realized I could do all the work I was doing by using {bStateSave: true} as an init parameter. Unfortunately when I enable sorting, the values in the cookies are not used, and iDisplayStart always defaults to zero still. Is there anything else I need to do?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi greattall1,

    You can pass "iDisplayStart" as an initialisation parameter: http://datatables.net/usage/options#iDisplayStart . Slightly confusing with two iDisplayStart parameters.... :-)

    Hope that helps,
    Allan
  • greattall1greattall1 Posts: 7Questions: 0Answers: 0
    Hey Allan,

    I initially was passing iDisplayStart as an initialization parameter from a value that I was reading out of a cookie. From what I can tell, bSaveState sets the value also when it is true. When I set bSort to true, my value for iDisplayStart is ignored.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi greattall1,

    Yes this is correct. The assumption was that if you have state saving enabled, then it would override the iDisplayStart initialisation parameter - since it would be typical to want to restore the last state fully.

    There are two ways around this for you:

    1. Change the code in DataTables - it's just a case of swapping to 'if' blocks around. Find the line "oSettings.iInitDisplayStart = oInit.iDisplayStart;" (near the end of the file) and put that code block below the state saving one, which is currently just after it.

    2. Once the table is drawn, set oTable.fnSettings()._iDisplayStart = {whatever}; and redraw the table. Probably a little easier - but not quite as slick :-)

    Regards,
    Allan
  • greattall1greattall1 Posts: 7Questions: 0Answers: 0
    I found the problem - it's on line 3507 of jquery.dataTables.js in _fnSort()

    oSettings._iDisplayStart = 0; /* reset display back to page 0 */

    This seems incorrect to me - _fnSort is called from the constructor. This means that the displayStart will always be reset to 0 when sorting is enabled, completely ignoring the saved state that we are trying to initialize the grid with (i.e. that the iDisplayStart != 0 because you are coming back to the page via browser back).

    The problem is, that you do want to reset iDisplayStart on the event of a new sort, because the result set will restart from the beginning again. Anyways, I will keep looking into this and post the solution I come up with.
  • greattall1greattall1 Posts: 7Questions: 0Answers: 0
    OK - here is my solution @ line 3157 in jquery.dataTables.js:

    if (oSettings.iServerDraw > 1) {
    oSettings._iDisplayStart = 0; /* reset display back to page 0 */
    }

    This works for me cuz I dont get my data until _fnDraw() is called because everything is server-side. Not sure if it works in all cases, tho.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    HI greattall1,

    The sort jumps the paging back to the start for usability reasons. It is possible to overrule DataTables in this matter by doing something exactly like you have done :-). It's perhaps an option that I should put into DataTables at some point.

    Regards,
    Allan
  • JamesLaiJamesLai Posts: 6Questions: 0Answers: 0
    Do any of these techniques still apply to dataTables 1.7.x? Unless I'm mistaken the codebase has changed enough to render these fixes unusable.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The iServerDraw parameter has been renamed to iDraw. Beyond that I don't think there are any changes which will effect this.

    Allan
  • morthahnamorthahna Posts: 18Questions: 0Answers: 0
    hey allan. could you add such functionality directly into datatables? doing fixes within the main datatables code doesn't look reliable to me, as you'll always have to reapply those patches after switching to a new release... :-/
  • morthahnamorthahna Posts: 18Questions: 0Answers: 0
    For the time beeing, this is how i come around this...

    Changed function _fnFilterComplete in datatables like this....

    /* Redraw the table */
    if (!oSettings.iStayOnPage == true) {
    oSettings._iDisplayStart = 0; /* reset display back to page 0 */
    }

    Doing the following in my script...

    oTable.fnSettings().iStayOnPage = true;
    oTable.fnUpdate(tags.join(","), aPos, 9);
    oTable.fnSettings().iStayOnPage = false;

    ...

    @allan: maybe it's a good idea to add such functionality to fnUpdate by a extra input parameter...
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes it sounds reasonable. I'll have a look at adding something like this in a future release.

    Allan
  • thoubothoubo Posts: 1Questions: 0Answers: 0
    Hi Allan,

    Did you finally add the "StayOnPage" feature ?

    Thanks in advance for your answer.
This discussion has been closed.