fnReloadAjax fix and feature

fnReloadAjax fix and feature

jeronejerone Posts: 12Questions: 0Answers: 0
edited March 2011 in General
I just adjusted the fnReloadAjax API function (http://www.datatables.net/plug-ins/api#fnReloadAjax) with a fix and a new feature.

The fix is a wrapper around the add data loop; json can be empty.

The feature is aborting the previous xhr request before creating a new one. For this feature I needed a custom fnServerData function that stored the xhr request in the settings. When fnReloadAjax is called again, the previous stored xhr request is then canceled first. It would be better if you implement the adjusted fnServerData, so we could access the xhr at all times.

The code below contains above explained fix and feature:
[code]
...
fnServerData: function(sSource, aoData, fnCallback) {
this.jqXHR = $.ajax({ ... });
}
...

($.fn && $.fn.dataTableExt && $.fn.dataTableExt.oApi && ($.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;

// Aborting the previous XHR;
if (oSettings.jqXHR) {
oSettings.jqXHR.abort();
}

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

// Check if json is not empty;
if (json) {
/* 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);

if (typeof bStandingRedraw != 'undefined' && bStandingRedraw === true) {
oSettings._iDisplayStart = iStart;
that.fnDraw(false);
}

that.oApi._fnProcessingDisplay(oSettings, false);

/* Callback user function - for event handlers etc */
if (typeof fnCallback == 'function' && fnCallback != null) {
fnCallback(oSettings);
}
});
}));
[/code]

Keep up the great work.

Replies

  • ardoramorardoramor Posts: 3Questions: 0Answers: 0
    Hmm, is there a new bug with dataTables 1.8.0 that has problems with jqXHR? I'm getting the following error in Chrome's console log:

    Uncaught TypeError: Cannot set property 'jqXHR' of undefined
    i.fn.dataTable.fnServerDatajquery.dataTables-1.8.0.min.js:38
    $.fn.dataTableExt.oApi.fnReloadAjaxdataTables.fnReloadAjax.js:16
    i.fn.dataTable.djquery.dataTables-1.8.0.min.js:40
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    In DataTables 1.8 fnServerData has been slightly updated to include a fourth parameter - the DataTables settings object. The XHR object from jQuery should be attached to that - as it is with the internal code. Looks like fnReloadAjax needs a small update to take account of that. I will take a look shortly.

    Allan
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Now done :-)

    Allan
  • hoodlumj3hoodlumj3 Posts: 5Questions: 0Answers: 0
    It would seem as though this plugin doesn't take into account dataTable 1.8.1's "sAjaxDataProp" option as it is getting the json data from oSettings.aaData (lines 16 and 18 here http://www.datatables.net/plug-ins/api#fnReloadAjax )
    One possible fix could be (which works for me) for lines 16-19
    [code]
    for (var i = 0; i < json[oSettings.sAjaxDataProp].length; i++)
    {
    that.oApi._fnAddData(oSettings, json[oSettings.sAjaxDataProp][i]);
    }
    [/code]

    apart from that Love your work!
This discussion has been closed.