From 1b6ffeaf782c518cd4ed2c07511dc49a6321e651 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Mon, 4 Feb 2013 19:44:41 +0000 Subject: [PATCH] Updated: `data` and `render` can now have periods escaped from dotted object notation. - Previously if you had an object key that contained a period, it wouldn't work with `data` or `render` (or rather it would need a function call to do it manually), since a split was being done on the periods to reconstruct the Javascript object property chain. Now it is possible to escape a period, allowing it to be included in the property name read / set. - Example: $('#example').dataTable( { columns: [ { data: 'a' }, { data: 'b\\.c' } ], data: [ { 'a': 1, 'b.c': 2 }, { 'a': 3, 'b.c': 4 }, { 'a': 5, 'b.c': 6 }, { 'a': 7, 'b.c': 8 } ] } ); --- media/src/core/core.data.js | 17 +++++++++++++++-- media/src/model/model.defaults.columns.js | 6 ++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/media/src/core/core.data.js b/media/src/core/core.data.js index b7bd0c6b..50bbc4a7 100644 --- a/media/src/core/core.data.js +++ b/media/src/core/core.data.js @@ -226,6 +226,19 @@ function _fnSetCellData( oSettings, iRow, iCol, val ) var __reArray = /\[.*?\]$/; var __reFn = /\(\)$/; +/** + * Split string on periods, taking into account escaped periods + * @param {string} str String to split + * @return {array} Split string + */ +function _fnSplitObjNotation( str ) +{ + return $.map( str.match(/(\\.|[^\.])+/g), function ( s ) { + return s.replace('\\.', '.'); + } ); +} + + /** * Return a function that can be used to get data from a source object, taking * into account the ability to use nested objects as a source @@ -258,7 +271,7 @@ function _fnGetObjectDataFn( mSource ) * be used if defined, rather than throwing an error */ var fetchData = function (data, type, src) { - var a = src.split('.'); + var a = _fnSplitObjNotation( src ); var arrayNotation, funcNotation, out, innerSrc; if ( src !== "" ) @@ -356,7 +369,7 @@ function _fnSetObjectDataFn( mSource ) { /* Like the get, we need to get data from a nested object */ var setData = function (data, val, src) { - var a = src.split('.'), b; + var a = _fnSplitObjNotation( src ); var aLast = a[a.length-1]; var arrayNotation, funcNotation, o, innerSrc; diff --git a/media/src/model/model.defaults.columns.js b/media/src/model/model.defaults.columns.js index f6c0473e..24097411 100644 --- a/media/src/model/model.defaults.columns.js +++ b/media/src/model/model.defaults.columns.js @@ -278,7 +278,8 @@ DataTable.defaults.column = { * * `.` - Dotted Javascript notation. Just as you use a `.` in * Javascript to read from nested objects, so to can the options * specified in `data`. For example: `browser.version` or - * `browser.name`. + * `browser.name`. If your object parameter name contains a period, use + * `\\` to escape it - i.e. `first\\.name`. * * `[]` - Array notation. DataTables can automatically combine data * from and array source, joining the data with the characters provided * between the two brackets. For example: `name[, ]` would provide a @@ -453,7 +454,8 @@ DataTable.defaults.column = { * * `.` - Dotted Javascript notation. Just as you use a `.` in * Javascript to read from nested objects, so to can the options * specified in `data`. For example: `browser.version` or - * `browser.name`. + * `browser.name`. If your object parameter name contains a period, use + * `\\` to escape it - i.e. `first\\.name`. * * `[]` - Array notation. DataTables can automatically combine data * from and array source, joining the data with the characters provided * between the two brackets. For example: `name[, ]` would provide a