diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 3283f6fe..2d5bbeed 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -804,7 +804,19 @@ */ 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 */ return function (data, type) { @@ -909,7 +921,16 @@ */ 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 */ return function (data, val) {}; @@ -10273,11 +10294,11 @@ /** * This property is the rendering partner to `data` and it is suggested that * when you want to manipulate data for display (including filtering, - * sorting etc) but not altering the underlying data for the table, use this - * property. `data` can actually do everything this property can and more, - * but this parameter is much easier to use as there is no 'set' option. - * Like `data` this option can be given in a number of different ways to - * effect its behaviour: + * sorting etc) without altering the underlying data for the table, use this + * property. `render` can be considered to be the the read only companion to + * `data` which is read / write (then as such more complex). Like `data` + * this option can be given in a number of different ways to effect its + * behaviour: * * * `integer` - treated as an array index for the data source. This is the * default that DataTables uses (incrementally increased for each column). @@ -10300,6 +10321,13 @@ * simple function on the data source, `browser.version()` for a * function in a nested property or even `browser().version` to get an * 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 * needs to set or get the data for a cell in the column. The function * takes three parameters: @@ -10313,8 +10341,8 @@ * * The return value from the function is what will be used for the * data requested. * - * @type string|int|function|null - * @default null _Use `data`_ + * @type string|int|function|object|null + * @default null Use the data source value. * * @name DataTable.defaults.column.render * @dtopt Columns @@ -10348,6 +10376,27 @@ * } ); * * @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 * $(document).ready( function() { * $('#example').dataTable( { diff --git a/media/src/core/core.data.js b/media/src/core/core.data.js index cf4443d6..c1a9c56b 100644 --- a/media/src/core/core.data.js +++ b/media/src/core/core.data.js @@ -248,7 +248,19 @@ function _fnSplitObjNotation( str ) */ 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 */ return function (data, type) { @@ -353,7 +365,16 @@ function _fnGetObjectDataFn( 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 */ return function (data, val) {}; diff --git a/media/src/model/model.defaults.columns.js b/media/src/model/model.defaults.columns.js index 5a4b670b..7fe7b436 100644 --- a/media/src/model/model.defaults.columns.js +++ b/media/src/model/model.defaults.columns.js @@ -440,11 +440,11 @@ DataTable.defaults.column = { /** * This property is the rendering partner to `data` and it is suggested that * when you want to manipulate data for display (including filtering, - * sorting etc) but not altering the underlying data for the table, use this - * property. `data` can actually do everything this property can and more, - * but this parameter is much easier to use as there is no 'set' option. - * Like `data` this option can be given in a number of different ways to - * effect its behaviour: + * sorting etc) without altering the underlying data for the table, use this + * property. `render` can be considered to be the the read only companion to + * `data` which is read / write (then as such more complex). Like `data` + * this option can be given in a number of different ways to effect its + * behaviour: * * * `integer` - treated as an array index for the data source. This is the * 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 * function in a nested property or even `browser().version` to get an * 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 * needs to set or get the data for a cell in the column. The function * takes three parameters: @@ -480,8 +487,8 @@ DataTable.defaults.column = { * * The return value from the function is what will be used for the * data requested. * - * @type string|int|function|null - * @default null _Use `data`_ + * @type string|int|function|object|null + * @default null Use the data source value. * * @name DataTable.defaults.column.render * @dtopt Columns @@ -515,6 +522,27 @@ DataTable.defaults.column = { * } ); * * @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 * $(document).ready( function() { * $('#example').dataTable( {