From 0c296ca76e3cc342d1ad78bf1f9bcf7f50f10dab Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 2 Sep 2011 16:01:56 +0100 Subject: [PATCH] New: fnServerParams callback function - this allows additional parameters to be added to the XHR for server-side processing or Ajax sourced, client-side processed data, with ease. Previously it was required to override the fnServerData method just to add a couple of parameters, but the built in fnServerData method is quite comperhensive and you don't want to have to reproduce all of that unless you need to. Now you don't need to :-). fnServerParams is called on each request, so it is ideal for adding extra parameters such as search parameters which can be updated by users. --- examples/server_side/custom_vars.html | 24 +++++---------- media/js/jquery.dataTables.js | 43 +++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/examples/server_side/custom_vars.html b/examples/server_side/custom_vars.html index 72dd11ea..7616afdb 100644 --- a/examples/server_side/custom_vars.html +++ b/examples/server_side/custom_vars.html @@ -17,13 +17,8 @@ "bProcessing": true, "bServerSide": true, "sAjaxSource": "scripts/server_processing.php", - "fnServerData": function ( sSource, aoData, fnCallback ) { - /* Add some extra data to the sender */ - aoData.push( { "name": "more_data", "value": "my_value" } ); - $.getJSON( sSource, aoData, function (json) { - /* Do whatever additional processing you want on the callback, then tell DataTables */ - fnCallback(json); - } ); + "fnServerParams": function ( aoData ) { + aoData.push( { "name": "more_data", "value": "my_value" } ); } } ); } ); @@ -36,7 +31,7 @@

Preamble

-

It can often be useful to send a little bit of extra information to the server when utilising DataTables' server-side processing option. This can be done by overriding the function used internally to get the JSON data from the server through the use of the "fnServerData" callback. This function takes three parameters (the same as $.getJSON(), to be manipulated as you wish. This allows you to add extra data to the second parameter and then make the AJAX call, or you can call a Google Gears DB, or whatever you want. This example shows adding a single extra HTTP variable.

+

It can often be useful to send a little bit of extra information to the server when utilising DataTables' server-side processing option. This can be done by using the fnServerParams callback function which is called whenever an XHR is sent to the server. fnServerParams Takes a single parameter, the array of name/value pairs of parameters that are to be sent to the server. You can manipulate this as you require - typically adding another parameter, as shown in this example.

Live example

@@ -74,15 +69,10 @@ $('#example').dataTable( { "bProcessing": true, "bServerSide": true, - "sAjaxSource": "scripts/server_processing.php", - "fnServerData": function ( sSource, aoData, fnCallback ) { - /* Add some extra data to the sender */ - aoData.push( { "name": "more_data", "value": "my_value" } ); - $.getJSON( sSource, aoData, function (json) { - /* Do whatever additional processing you want on the callback, then tell DataTables */ - fnCallback(json) - } ); - } + "sAjaxSource": "scripts/server_processing.php" + "fnServerParams": function ( aoData ) { + aoData.push( { "name": "more_data", "value": "my_value" } ); + } } ); } ); diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 6be5bdfe..155a48bc 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -25,7 +25,7 @@ * When considering jsLint, we need to allow eval() as it it is used for reading cookies */ /*jslint evil: true, undef: true, browser: true */ -/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageProcess,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnArrayCmp,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn*/ +/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageProcess,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnArrayCmp,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn*/ (function($, window, document) { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -1349,6 +1349,17 @@ } ); }; + /* + * Variable: aoServerParams + * Purpose: Functions which are called prior to sending an Ajax request so extra parameters + * can easily be sent to the server + * Scope: jQuery.dataTable.classSettings + * Notes: Each array element is an object with the following parameters: + * function:fn - function to call + * string:sName - name callback - useful for knowing where it came from (plugin etc) + */ + this.aoServerParams = []; + /* * Variable: fnFormatNumber * Purpose: Format numbers for display @@ -2397,7 +2408,9 @@ /* if there is an ajax source load the data */ if ( oSettings.sAjaxSource !== null && !oSettings.oFeatures.bServerSide ) { - oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, [], function(json) { + var aoData = []; + _fnServerParams( oSettings, aoData ); + oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData, function(json) { var aData = json; if ( oSettings.sAjaxDataProp !== "" ) { @@ -3429,6 +3442,7 @@ _fnProcessingDisplay( oSettings, true ); var iColumns = oSettings.aoColumns.length; var aoData = _fnAjaxParameters( oSettings ); + _fnServerParams( oSettings, aoData ); oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData, function(json) { @@ -3507,6 +3521,21 @@ return aoData; } + /* + * Function: _fnServerParams + * Purpose: Add Ajax parameters from plugins + * Returns: - + * Inputs: object:oSettings - dataTables settings object + * array objects:aoData - name/value pairs to send to the server + */ + function _fnServerParams( oSettings, aoData ) + { + for ( var i=0, iLen=oSettings.aoServerParams.length ; i