fnReloadAjax fix and feature
fnReloadAjax fix and feature
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.
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.
This discussion has been closed.
Replies
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
Allan
Allan
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!