1
0
mirror of https://github.com/DataTables/DataTables.git synced 2024-11-29 11:24:10 +01:00

New / update: The data option, when used as a function, is given and

can return only objects now.

- The new `ajax` option for 1.10 did previously allow both arrays of
  objects with name value pairs and objects, however, this change
  removes the option of using arrays of objects to try and simplify
  things. One of the most common questions in the forums is about name
  value pair objects, so this sidesteps that.

- The big benefit of doing this is that the data passed into `data()` is
  now very easy to manipulate. Rather than needing to loop over the
  array, you can just modify the parameter you want.

- It also allows an object to be passed back from the function,
  manipulated as desired. For example it is now super easy to nest the
  DataTables parameters in a sub-object:

	$('#example').dataTable( {
		'processing': true,
		'serverSide': true,
		'ajax': {
			'url': '../server_side/scripts/server_processing.php',
			'data': function ( data ) {
				return { 'myprop': data };
			}
		}
	} );

  This fixed issue #124.

- The parameters submitted to the server and those expected back are
  currently left as is. They will be updated to be camelCase at some
  point, but not yet sure if that will be for 1.10 or 1.11.
This commit is contained in:
Allan Jardine 2013-06-21 09:26:51 +01:00
parent 76e572985d
commit 370a1e7f7a
2 changed files with 23 additions and 25 deletions

View File

@ -14,6 +14,13 @@ function _fnBuildAjax( oSettings, data, fn )
// Compatibility with 1.9-, allow fnServerData and event to manipulate // Compatibility with 1.9-, allow fnServerData and event to manipulate
_fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [data] ); _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [data] );
// Convert to object based for 1.10+
var tmp = {};
$.each( data, function (key, val) {
tmp[val.name] = val.value;
} );
data = tmp;
var ajaxData; var ajaxData;
var ajax = oSettings.ajax; var ajax = oSettings.ajax;
var instance = oSettings.oInstance; var instance = oSettings.oInstance;
@ -21,28 +28,18 @@ function _fnBuildAjax( oSettings, data, fn )
if ( $.isPlainObject( ajax ) && ajax.data ) if ( $.isPlainObject( ajax ) && ajax.data )
{ {
ajaxData = ajax.data; ajaxData = ajax.data;
var newData = $.isFunction( ajaxData ) ? var newData = $.isFunction( ajaxData ) ?
ajaxData( data ) : // fn can manipulate data or return an object or array ajaxData( data ) : // fn can manipulate data or return an object
ajaxData; // object or array to merge ajaxData; // object or array to merge
if ( $.isArray( newData ) ) // If the function returned an object, use that alone
{ data = $.isFunction( ajaxData ) && newData ?
// name value pair objects in an array newData :
data = data.concat( newData ); $.extend( true, data, newData );
}
else if ( $.isPlainObject( newData ) )
{
// aData is an array of name value pairs at this point - convert to
// an object to easily merge data - jQuery will cope with the switch
var oData = {};
$.each( data, function (key, val) {
oData[val.name] = val.value;
} );
data = $.extend( true, oData, newData ); // Remove the data property as we've resolved it already and don't want
} // jQuery to do it again (it is restored at the end of the function)
// Remove the data property as we've resolved it already
delete ajax.data; delete ajax.data;
} }

View File

@ -164,13 +164,14 @@ DataTable.defaults = {
* the following parameters provide additional options in DataTables or * the following parameters provide additional options in DataTables or
* require special consideration: * require special consideration:
* *
* * `data` - As with jQuery, `data` can be provided as an object or array * * `data` - As with jQuery, `data` can be provided as an object, but it
* of name/value pairs, but it can also be used as a function to * can also be used as a function to manipulate the data DataTables sends
* manipulate the data DataTables sends to the server. The function takes * to the server. The function takes a single parameter, an object of
* a single parameter, an array of name/value pairs that DataTables has * parameters with the values that DataTables has readied for sending. An
* readied for sending. An object or array can be returned which will be * object may be returned which will be merged into the DataTables
* merged into the DataTables defaults, or you can add the items to the * defaults, or you can add the items to the object that was passed in and
* array passed in. This supersedes `fnServerParams` from DataTables 1.9-. * not return anything from the function. This supersedes `fnServerParams`
* from DataTables 1.9-.
* *
* * `dataSrc` - By default DataTables will look for the property 'aaData' * * `dataSrc` - By default DataTables will look for the property 'aaData'
* when obtaining data from an Ajax source or for server-side processing - * when obtaining data from an Ajax source or for server-side processing -