How to set current page when using server-side processing?

How to set current page when using server-side processing?

zharbenzharben Posts: 3Questions: 0Answers: 0
edited April 2011 in General
First off, I love what you've created, and donated this morning. You've saved me a ton of time trying to hack together various libraries, when yours does almost everything.

I am using datatables with server-side processing and pagination. A user can select a single row, and take some action. When the user returns to the page, the previously selected row needs to be highlighted, and on screen.

Currently, we are locating the record on the server side, and returning the appropriate page of results to the datatable via AJAX. However, I can't see any method of updating the current page selection to show which page i'm on. It always defaults to "1", even if I've returned the 3rd page of results.

I can't use state-saving, because the list that will display in the table may have changed since the user last visited it - e.g. the page that their selection appears on may have changed.

I can't see any data returned within the standard 'server-side processing' reply that would tell me what the current page is, and I can't append my own parameters to the standard reply, because there's no way to access them via callbacks.

Unless you have an easy solution, one of the two following improvements would solve this problem for us :
* Add a callback that has access to the JSON response from the server - e.g. :
[code]
function _fnAjaxUpdate( oSettings )
{
...
oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData,
function(json) {
_fnAjaxUpdateDraw( oSettings, json );

/* trigger the server reply callback, if there is one */
if ( typeof oSettings.fnServerReplyCallback == 'function' )
{
oSettings.fnServerReplyCallback.call(oSettings, json);
}

} );
...
[/code]
** this would allow users to insert custom parameters in the JSON and use them to modify table settings, such as current page
* Add a (optional?) parameter to the server reply format that specifies the starting index or current page of results

For now, I will have the server set a cookie with the page that the datatable should indicate in the pagination widget, and access that cookie in the fnDrawCallback function.

Replies

  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi zharben,

    Thanks for the detailed description. From what you describe, you want to be able to set the start page when the table is initialised (i.e. the page reloaded), is that correct? This can be done using this parameter: http://datatables.net/usage/options#iDisplayStart .

    Furthermore, there is a plug-in API function which can be used to change the display start point after initialisation: http://datatables.net/plug-ins/api#fnDisplayStart

    Will that do what you are looking for?

    Allan
  • zharbenzharben Posts: 3Questions: 0Answers: 0
    Thanks for your quick response, Allan!

    Not quite. Here's the sequence of events :
    1) User selects a record on page 2; the selection is cookied
    2) User visits some other page
    3) User returns to the page with the datatable.
    3a) The page that loads the datatable does not know what page of results the cookied selection is on
    4) The datatable calls the server-side URL
    4a) the server detects the selected record cookie, retrieves the corresponding a page of records and returns the standard JSON response
    5) The datatable renders the results

    I could modify the code so that at step (3a), the page knows what page of results needs to be loaded, but that requires passing through the same sets of queries/logic twice - at (3a) and (4a). In other words, I would need to look up the correct page in (3a), and in (4a) look up and return the page of results.

    I would prefer that I do all of the necessary processing in (4a), and be able to pass the necessary information (the startIndex, or current page) within the JSON response. This is what I was suggesting at the end of my last comment. As far as I can tell, there are no hooks in the server response format to pass the current page/startIndex to the dataTable.

    * I suppose another approach would be to load the page of data in (3a), using an on-page data source, then switch the dataTable back to using server-side processing after it is rendered. Not sure I like that approach.

    Thanks!
    Zeph
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi Zeph,

    Hmmmm - good one. If it were client-side you could use this plug-in: http://datatables.net/plug-ins/api#fnDisplayRow . But given that it is server-side that won't work...

    Is the page holding the table dynamically generated (PHP or whatever)? Would it be possible when the page is loaded to lookup what page the user should be put on and write the iDisplayStart property to the table initialisation? That way there wouldn't need to be any modification to the server-side processing script.

    An alternative might be to make an Ajax call to find out what page the user should be put on, prior to initialising the table and then brining it up with iDisplayStart set. Or it might be possible to use fnServerData to intercept the call (which I think it more or less what you are suggesting with the callback) and that could modify the DataTables' settings based on the returned 'start point'.

    Regards,
    Allan
  • zharbenzharben Posts: 3Questions: 0Answers: 0
    Thanks for your guidance Allan. We're working through the kinks. We used your suggestion to modify fnServerData to intercept the server response and that helped signicantly.
This discussion has been closed.