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

New: Extend render option to use objects allowing different data to be

used for the different data types very easily.

- Until now, if you want to use different data for the different data
  types (I've called these orthogonal data in relations to DataTables)
  you had to specify a function. That's fine, but it seems a rather
  clumsy way of just pulling different data out of a source object based
  on the type. This method allows the data types to be very easily
  defined with an object, allowing the same rules as `render` normally
  does (dotted object notation, array notation etc).

- For example:

    $(document).ready(function() {
    	$('#example').dataTable( {
    		columns: [
    			{ data: null, render: {
    				_: 'a',
    				sort: 'c',
    				type: 'c',
    				filter: 'd'
    			} },
    			{ data: 'b' }
    		],
    		data: [
    			{ 'a': 1, 'b': 2, 'c': 4, 'd': '1' },
    			{ 'a': 3, 'b': 4, 'c': 3, 'd': '3' },
    			{ 'a': 5, 'b': 6, 'c': 2, 'd': '5' },
    			{ 'a': 7, 'b': 8, 'c': 1, 'd': 'allan' }
    		]
    	} );
    } );
This commit is contained in:
Allan Jardine 2013-03-02 18:53:27 +00:00
parent 079a1f2e1d
commit 2bd643468b
3 changed files with 116 additions and 18 deletions

View File

@ -804,7 +804,19 @@
*/ */
function _fnGetObjectDataFn( mSource ) function _fnGetObjectDataFn( mSource )
{ {
if ( mSource === null ) if ( $.isPlainObject( mSource ) )
{
/* Build an object of get functions, and wrap them in a single call */
var o = {};
$.each( mSource, function (key, val) {
o[key] = _fnGetObjectDataFn( val );
} );
return function (data, type, extra) {
return o[ o[type] !== undefined ? type : '_' ](data, type, extra);
};
}
else if ( mSource === null )
{ {
/* Give an empty string for rendering / sorting etc */ /* Give an empty string for rendering / sorting etc */
return function (data, type) { return function (data, type) {
@ -909,7 +921,16 @@
*/ */
function _fnSetObjectDataFn( mSource ) function _fnSetObjectDataFn( mSource )
{ {
if ( mSource === null ) if ( $.isPlainObject( mSource ) )
{
/* Unlike get, only the underscore (global) option is used for for
* setting data since we don't know the type here. This is why an object
* option is not documented for `mData` (which is read/write), but it is
* for `mRender` which is read only.
*/
return _fnSetObjectDataFn( mSource._ );
}
else if ( mSource === null )
{ {
/* Nothing to do when the data source is null */ /* Nothing to do when the data source is null */
return function (data, val) {}; return function (data, val) {};
@ -10273,11 +10294,11 @@
/** /**
* This property is the rendering partner to `data` and it is suggested that * This property is the rendering partner to `data` and it is suggested that
* when you want to manipulate data for display (including filtering, * when you want to manipulate data for display (including filtering,
* sorting etc) but not altering the underlying data for the table, use this * sorting etc) without altering the underlying data for the table, use this
* property. `data` can actually do everything this property can and more, * property. `render` can be considered to be the the read only companion to
* but this parameter is much easier to use as there is no 'set' option. * `data` which is read / write (then as such more complex). Like `data`
* Like `data` this option can be given in a number of different ways to * this option can be given in a number of different ways to effect its
* effect its behaviour: * behaviour:
* *
* * `integer` - treated as an array index for the data source. This is the * * `integer` - treated as an array index for the data source. This is the
* default that DataTables uses (incrementally increased for each column). * default that DataTables uses (incrementally increased for each column).
@ -10300,6 +10321,13 @@
* simple function on the data source, `browser.version()` for a * simple function on the data source, `browser.version()` for a
* function in a nested property or even `browser().version` to get an * function in a nested property or even `browser().version` to get an
* object property if the function called returns an object. * object property if the function called returns an object.
* * `object` - use different data for the different data types requested by
* DataTables ('filter', 'display', 'type' or 'sort'). The property names
* of the object is the data type the property refers to and the value can
* defined using an integer, string or function using the same rules as
* `render` normally does. Note that an `_` option _must_ be specified.
* This is the default value to use if you haven't specified a value for
* the data type requested by DataTables.
* * `function` - the function given will be executed whenever DataTables * * `function` - the function given will be executed whenever DataTables
* needs to set or get the data for a cell in the column. The function * needs to set or get the data for a cell in the column. The function
* takes three parameters: * takes three parameters:
@ -10313,8 +10341,8 @@
* * The return value from the function is what will be used for the * * The return value from the function is what will be used for the
* data requested. * data requested.
* *
* @type string|int|function|null * @type string|int|function|object|null
* @default null _Use `data`_ * @default null Use the data source value.
* *
* @name DataTable.defaults.column.render * @name DataTable.defaults.column.render
* @dtopt Columns * @dtopt Columns
@ -10348,6 +10376,27 @@
* } ); * } );
* *
* @example * @example
* // As an object, extracting different data for the different types
* // This would be used with a data source such as:
* // { "phone": 5552368, "phone_filter": "5552368 555-2368", "phone_display": "555-2368" }
* // Here the `phone` integer is used for sorting and type detection, while `phone_filter`
* // (which has both forms) is used for filtering for if a user inputs either format, while
* // the formatted phone number is the one that is shown in the table.
* $(document).ready( function() {
* $('#example').dataTable( {
* "columnDefs": [ {
* "targets": [ 0 ],
* "data": null, // Use the full data source object for the renderer's source
* "render": {
* "_": "phone",
* "filter": "phone_filter",
* "display": "phone_display"
* }
* } ]
* } );
* } );
*
* @example
* // Use as a function to create a link from the data source * // Use as a function to create a link from the data source
* $(document).ready( function() { * $(document).ready( function() {
* $('#example').dataTable( { * $('#example').dataTable( {

View File

@ -248,7 +248,19 @@ function _fnSplitObjNotation( str )
*/ */
function _fnGetObjectDataFn( mSource ) function _fnGetObjectDataFn( mSource )
{ {
if ( mSource === null ) if ( $.isPlainObject( mSource ) )
{
/* Build an object of get functions, and wrap them in a single call */
var o = {};
$.each( mSource, function (key, val) {
o[key] = _fnGetObjectDataFn( val );
} );
return function (data, type, extra) {
return o[ o[type] !== undefined ? type : '_' ](data, type, extra);
};
}
else if ( mSource === null )
{ {
/* Give an empty string for rendering / sorting etc */ /* Give an empty string for rendering / sorting etc */
return function (data, type) { return function (data, type) {
@ -353,7 +365,16 @@ function _fnGetObjectDataFn( mSource )
*/ */
function _fnSetObjectDataFn( mSource ) function _fnSetObjectDataFn( mSource )
{ {
if ( mSource === null ) if ( $.isPlainObject( mSource ) )
{
/* Unlike get, only the underscore (global) option is used for for
* setting data since we don't know the type here. This is why an object
* option is not documented for `mData` (which is read/write), but it is
* for `mRender` which is read only.
*/
return _fnSetObjectDataFn( mSource._ );
}
else if ( mSource === null )
{ {
/* Nothing to do when the data source is null */ /* Nothing to do when the data source is null */
return function (data, val) {}; return function (data, val) {};

View File

@ -440,11 +440,11 @@ DataTable.defaults.column = {
/** /**
* This property is the rendering partner to `data` and it is suggested that * This property is the rendering partner to `data` and it is suggested that
* when you want to manipulate data for display (including filtering, * when you want to manipulate data for display (including filtering,
* sorting etc) but not altering the underlying data for the table, use this * sorting etc) without altering the underlying data for the table, use this
* property. `data` can actually do everything this property can and more, * property. `render` can be considered to be the the read only companion to
* but this parameter is much easier to use as there is no 'set' option. * `data` which is read / write (then as such more complex). Like `data`
* Like `data` this option can be given in a number of different ways to * this option can be given in a number of different ways to effect its
* effect its behaviour: * behaviour:
* *
* * `integer` - treated as an array index for the data source. This is the * * `integer` - treated as an array index for the data source. This is the
* default that DataTables uses (incrementally increased for each column). * default that DataTables uses (incrementally increased for each column).
@ -467,6 +467,13 @@ DataTable.defaults.column = {
* simple function on the data source, `browser.version()` for a * simple function on the data source, `browser.version()` for a
* function in a nested property or even `browser().version` to get an * function in a nested property or even `browser().version` to get an
* object property if the function called returns an object. * object property if the function called returns an object.
* * `object` - use different data for the different data types requested by
* DataTables ('filter', 'display', 'type' or 'sort'). The property names
* of the object is the data type the property refers to and the value can
* defined using an integer, string or function using the same rules as
* `render` normally does. Note that an `_` option _must_ be specified.
* This is the default value to use if you haven't specified a value for
* the data type requested by DataTables.
* * `function` - the function given will be executed whenever DataTables * * `function` - the function given will be executed whenever DataTables
* needs to set or get the data for a cell in the column. The function * needs to set or get the data for a cell in the column. The function
* takes three parameters: * takes three parameters:
@ -480,8 +487,8 @@ DataTable.defaults.column = {
* * The return value from the function is what will be used for the * * The return value from the function is what will be used for the
* data requested. * data requested.
* *
* @type string|int|function|null * @type string|int|function|object|null
* @default null _Use `data`_ * @default null Use the data source value.
* *
* @name DataTable.defaults.column.render * @name DataTable.defaults.column.render
* @dtopt Columns * @dtopt Columns
@ -515,6 +522,27 @@ DataTable.defaults.column = {
* } ); * } );
* *
* @example * @example
* // As an object, extracting different data for the different types
* // This would be used with a data source such as:
* // { "phone": 5552368, "phone_filter": "5552368 555-2368", "phone_display": "555-2368" }
* // Here the `phone` integer is used for sorting and type detection, while `phone_filter`
* // (which has both forms) is used for filtering for if a user inputs either format, while
* // the formatted phone number is the one that is shown in the table.
* $(document).ready( function() {
* $('#example').dataTable( {
* "columnDefs": [ {
* "targets": [ 0 ],
* "data": null, // Use the full data source object for the renderer's source
* "render": {
* "_": "phone",
* "filter": "phone_filter",
* "display": "phone_display"
* }
* } ]
* } );
* } );
*
* @example
* // Use as a function to create a link from the data source * // Use as a function to create a link from the data source
* $(document).ready( function() { * $(document).ready( function() {
* $('#example').dataTable( { * $('#example').dataTable( {