fnReloadAjax with fnStandingRedraw

fnReloadAjax with fnStandingRedraw

KFAKevinKFAKevin Posts: 4Questions: 0Answers: 0
edited December 2010 in General
New to DataTables, and so far very pleased with what it can do. I am having one minor issue and after searching the forums and trying a few things am unable to resolve this issue.

I load data for the table via a PHP script which is called in the sAjaxSource setting, I run a reload of the data every 30 secs to get any changes or new records via fnReloadAjax on a javascript timer. However when it reloads it dumps you back to page 1 of the table. How do I get it to retain the current page that your on?

Here is my code:
[code]
$(document).ready(function () {
inMsg = $('.inboundmsg').dataTable( {
'bProcessing': true,
'bLengthChange': true,
'bPaginate': true,
'sPaginationType': 'full_numbers',
'iDisplayLength': 5,
'bInfo': false,
'bStandingRedraw': true,
'aaSorting': [[ 5, "desc" ]],
'oLanguage':
{
'sSearch': 'Search:',
'sProcessing': ' Refreshing Data',
'oPaginate':
{
'sNext': '>',
'sLast': '>>',
'sFirst': '<<',
'sPrevious': '<'
}
},
'aoColumns': [
{ "bSortable": false },
{ "sClass": "bold left" },
{ "sClass": "sixtysix left" },
{ "sClass": "value center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center"},
{ "bSortable": false, "sClass": "full nowrap center" }
],
"sAjaxSource": 'includes/datacalls/DataHelper.php?action=inboundmsg&pnetid=74148&showstatus=1'
} );

setTimeout(function() { reloadTable(inMsg); },30000);

});
[/code]

Here is a copy of the simpe reloadTable function I call
[code]
var tblName;
function reloadTable(tblName){
tblName.fnReloadAjax();
setTimeout(function() { reloadTable(tblName); },30000);
}
[/code]

Any help or suggestions would be greatly appreciated.

Replies

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    The reason that DataTables resets back to page 1 is that the new data from the server might be different, so it so a complete redraw. Not ideal in a case like your own though! So what you'll probably need to do is something a bit like http://datatables.net/plug-ins/api#fnStandingRedraw - fnAjaxReload as a callback function - you can make use of that to set the page (like in the standing redraw method) and then draw the table again with the new page selected.

    Allan
  • KFAKevinKFAKevin Posts: 4Questions: 0Answers: 0
    Sorry not much of a javascript developer more php then anything, I am not finding much help from that page.
    I have added the plugin for standingRedraw but not seeing any difference.

    What is this part of the fnReloadAjax do? from what I am reading it looks like if bStandingRedraw is true then it should not be jumping back to page 1.

    [code]
    if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
    {
    oSettings._iDisplayStart = iStart;
    that.fnDraw( false );
    }
    [/code]
  • KFAKevinKFAKevin Posts: 4Questions: 0Answers: 0
    Ok a little tweaking comparing the two plugins and I have it working....
    I get data refreshing and remaining on the same page.

    I have tested this with editing existing records, will test tomorrow with a new record being added.

    By adding the following lines
    [code]
    oSettings._iDisplayStart = iStart;
    oSettings.oApi._fnCalculateEnd(oSettings)
    that.fnDraw( false );
    [/code]

    Modified fnReloadAjax:
    [code]
    $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
    {
    if ( typeof sNewSource != 'undefined' && sNewSource != null )
    {
    oSettings.sAjaxSource = sNewSource;
    }
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;

    oSettings.fnServerData( oSettings.sAjaxSource, null, function(json) {
    /* Clear the old information from the table */
    that.oApi._fnClearTable( oSettings );

    /* Got the data - add it to the table */
    for ( var i=0 ; i
  • KFAKevinKFAKevin Posts: 4Questions: 0Answers: 0
    Tested with a new record added and also works perfect.....
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Very cool nice one :-). I'll get it up on the plug-ins page soon :-)

    Allan
This discussion has been closed.