help with fnDisplayStart()

help with fnDisplayStart()

firefall6firefall6 Posts: 3Questions: 0Answers: 0
edited August 2013 in Plug-ins
I'm having trouble getting this routine to reset the start page. I'm using bserverside = false and bstatesave = true, meaning the JSON data gets retrieved once, the user can paginate through it client side and the current page is remembered so if you leave the page and come back you're on the same page. The problem is that in some cases I want to put the user back to page 0. Here's my code so far:

$(document).ready(function () {
var oPaymentsTable = $('#payments').dataTable({
"bJQueryUI": true,
"bServerSide": false,
"sAjaxSource": "myAjaxSource",
"sPaginationType": "full_numbers",
"bStateSave": true
})

var oTable = $('#payments').dataTable();
oTable.fnDisplayStart(0); //this will be a property of the model, but I'm assuming 0 for now

$.fn.dataTableExt.oApi.fnDisplayStart = function ( oSettings, iStart, bRedraw )
{
if ( typeof bRedraw == 'undefined' )
{
bRedraw = true;
}

oSettings._iDisplayStart = iStart;
oSettings.oApi._fnCalculateEnd( oSettings );

alert(iStart); // this is correctly 0
if ( bRedraw )
{
alert('redraw'); // I get to here
oSettings.oApi._fnDraw( oSettings );
}
};

The bStateSave is working fine, and the table goes back to the page they were on. The problem is if I come through fresh from the server and detect that I need to reset the start page, it still shows the page that's saved in the cookie from bStateSaved. The alerts tell me I'm in the plug-in code, and I get to the fnDraw, but I never get back to page 0.

In 1.7.5 I was setting _iDisplayStart directly (without the plug-in) and it was working fine, but since I switched to 1.9.4 I can't get it to reset the page to 0. I feel like I must be doing something out of order or missing a step. Thanks in advance for any help.

-Harriet

Replies

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    Try doing it in fnInitComplete . Or possibly you could try `fnPageChange( 'first' )` (which is built in - no plug-in needed).

    Allan
  • firefall6firefall6 Posts: 3Questions: 0Answers: 0
    Thanks for the quick reply. Regarding fnPageChange( 'first' ) - I still feel like I'm doing stuff in the wrong place. Inside $(document).ready(function ()...) I have the setup for the table as I wrote above, and then immediately following I put

    var oTable = $('#payments').dataTable();
    oTable.fnPageChange( 'first' );
    alert('past first');

    I get the alert so I know it gets there, but the page is still on the one saved to the cookie by bStateSave, not the first page.
  • firefall6firefall6 Posts: 3Questions: 0Answers: 0
    This worked for me (I wrapped it in an if statement that looks at my model property to decide whether to reset to 0):

    "fnInitComplete": function () {
    this.fnPageChange( 'first' );
    }

    So I have to assume my issue all along has been that the table wasn't "ready".

    Thanks for your help Allan. - Harriet
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    Yes, fnInitComplete is the way to do it from the looks of things.

    Good to hear it works for you now.

    Allan
This discussion has been closed.