Ajax Reload without jumping to the first page

Ajax Reload without jumping to the first page

MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
edited June 2010 in General
It's nearly similar to that post:
http://datatables.net/forums/comments.php?DiscussionID=643&page=1

I use also the function fnReloadAjax;

But it is necessary that after realoading the data the page keeps on the same like before. It should not jump to the first page.
How can I achieve this?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi MathiasWeisheit,

    What you can do is something like this:

    [code]
    var iCurrentPage = oTable.fnSettings()._iDisplayStart;
    oTable.fnReloadAjax( null, function (o) {
    o._iDisplayStart = iCurrentPage;
    oTable.fnDraw();
    } );
    [/code]

    I've also made a small update to the fnReloadAjax plug-in to allow for this (it needed a null check): http://datatables.net/plug-ins/api#fnReloadAjax

    Let me know how you get on with that :-). I'll have a look at including this into the fnReloadAjax plug-in in future.

    Regards,
    Allan
  • MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
    edited June 2010
    [code]
    var oCurrentSettings = CDL.jqDataTable.fnSettings();
    var iCurrentPage = oCurrentSettings._iDisplayStart;

    CDL.jqDataTable.fnReloadAjax( null, function (o) {
    o._iDisplayStart = iCurrentPage;
    CDL.jqDataTable.fnDraw();
    });
    [/code]

    now jquery raises an error e.url is null;

    my AjaxReload Function:
    [code]
    $.fn.dataTableExt.oApi.fnReloadAjax = function(oSettings, sNewSource,
    fnCallback) {

    if (typeof sNewSource != 'undefined' && sNewSource != '') {
    oSettings.sAjaxSource = sNewSource;
    }
    this.oApi._fnProcessingDisplay(oSettings, true);
    var that = this;

    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 < json.aaData.length; i++) {
    that.oApi._fnAddData(oSettings, json.aaData[i]);
    }

    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    that.fnDraw(that);
    that.oApi._fnProcessingDisplay(oSettings, false);

    /* Callback user function - for event handlers etc */
    if (typeof fnCallback == 'function') {
    fnCallback(oSettings);
    }
    });
    };
    [/code]
  • MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
    My last code was:
    [code]
    var oSettings = CDL.jqDataTable.fnSettings();
    var iCurrentDisplayLength = oSettings._iDisplayLength;
    var iCurrentDisplayStart = oSettings._iDisplayStart;
    CDL.jqDataTable.fnReloadAjax();
    oSettings._iDisplayLength = iCurrentDisplayLength ;
    oSettings._iDisplayStart = iCurrentDisplayStart;
    CDL.jqDataTable.fnDraw(oSettings);
    //CDL.jqDataTable.oApi._fnDraw(oSettings);
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I think the problem is this line in your fnReloadAjax:

    [code]
    if (typeof sNewSource != 'undefined' && sNewSource != '') {
    [/code]

    null is not the same as '' (which is an empty string - null is an absence of data), and as such 'null' is being assigned to the DataTables Ajax source parameter - which I would say is the cause for the error you are seeing. If you change the if condition to test for null (or pass in a blank string rather than null) this hopefully it will work as expected :-)

    Regards,
    Allan
  • MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
    You are right. But it doesn't works. He jumps to the first page!
    I also tried to set the settings-object directly.

    [code]
    var oCurrentSettings = CDL.jqDataTable.fnSettings();
    var iCurrentPage = oCurrentSettings._iDisplayStart;

    CDL.jqDataTable.fnReloadAjax( '', function (o) {
    o._iDisplayStart = iCurrentPage;
    CDL.jqDataTable.dataTableSettings[0]._iDisplayStart = iCurrentPage;
    CDL.jqDataTable.fnDraw();
    });
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I've just updated the fnReloadAjax plug-in to allow standing redraw... All you need to do is call it like this:

    [code]
    oTable.fnReloadAjax( null, null, true );
    [/code]
    The new part is the third parameter which when true does a standing redraw. If you copy the new plug-in source, hopefully this will do the job for you!

    Allan
  • MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
    I tried the same yesterday, but it does'nt work. But your code is perfekt!
    It works.

    I'll try to test it in the complete application.
  • chandruchandru Posts: 17Questions: 0Answers: 0
    Hi,
    Use this "bStateSave": true
  • MathiasWeisheitMathiasWeisheit Posts: 7Questions: 0Answers: 0
    bStateSave works not with ajax reload.

    The function from allan works!
This discussion has been closed.