From fd0e0a42e4ca01cc0cc2c8df4bcff3dd72c37576 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Wed, 8 Aug 2012 20:16:40 +0100 Subject: [PATCH] Fix #39 - null values should also be considered like undefined values when working with nested data and have properties created as needed. --- media/js/jquery.dataTables.js | 6 +- media/src/core/core.data.js | 6 +- .../tests_onhold/2_js/39-nested-null.js | 73 +++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 media/unit_testing/tests_onhold/2_js/39-nested-null.js diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index f25a7e8e..7df8f29b 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -867,11 +867,11 @@ break; } - data = data[ a[i] ]; - if ( data === undefined ) + if ( data === null || data[ a[i] ] === undefined ) { return undefined; } + data = data[ a[i] ]; } } @@ -949,7 +949,7 @@ // If the nested object doesn't currently exist - since we are // trying to set the value - create it - if ( data[ a[i] ] === undefined ) + if ( data[ a[i] ] === null || data[ a[i] ] === undefined ) { data[ a[i] ] = {}; } diff --git a/media/src/core/core.data.js b/media/src/core/core.data.js index 8e51333f..a35a919d 100644 --- a/media/src/core/core.data.js +++ b/media/src/core/core.data.js @@ -433,11 +433,11 @@ function _fnGetObjectDataFn( mSource ) break; } - data = data[ a[i] ]; - if ( data === undefined ) + if ( data === null || data[ a[i] ] === undefined ) { return undefined; } + data = data[ a[i] ]; } } @@ -515,7 +515,7 @@ function _fnSetObjectDataFn( mSource ) // If the nested object doesn't currently exist - since we are // trying to set the value - create it - if ( data[ a[i] ] === undefined ) + if ( data[ a[i] ] === null || data[ a[i] ] === undefined ) { data[ a[i] ] = {}; } diff --git a/media/unit_testing/tests_onhold/2_js/39-nested-null.js b/media/unit_testing/tests_onhold/2_js/39-nested-null.js new file mode 100644 index 00000000..dd2974a2 --- /dev/null +++ b/media/unit_testing/tests_onhold/2_js/39-nested-null.js @@ -0,0 +1,73 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "39 - nested null values" ); + +$(document).ready( function () { + var test = false; + + $.fn.dataTable.ext.sErrMode = "throw"; + + oTest.fnTest( + "No default content throws an error", + function () { + try { + $('#example').dataTable( { + "aaData": [ + { "a": "0", "b": {"c": 0} }, + { "a": "1", "b": {"c": 3} }, + { "a": "2", "b": null } + ], + "aoColumns": [ + { "mDataProp": "a" }, + { "mDataProp": "b.c" } + ] + } ); + } + catch(err) { + test = true; + } + }, + function () { return test; } + ); + + oTest.fnTest( + "Table renders", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "aaData": [ + { "a": "0", "b": {"c": 0} }, + { "a": "1", "b": {"c": 3} }, + { "a": "2", "b": null } + ], + "aoColumns": [ + { "mDataProp": "a" }, + { "mDataProp": "b.c", "sDefaultContent": "allan" } + ] + } ); + }, + function () { return $('#example tbody td:eq(0)').html() === "0"; } + ); + + oTest.fnTest( + "Default content applied", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "aaData": [ + { "a": "0", "b": {"c": 0} }, + { "a": "1", "b": {"c": 3} }, + { "a": "2", "b": null } + ], + "aoColumns": [ + { "mDataProp": "a" }, + { "mDataProp": "b.c", "sDefaultContent": "allan" } + ] + } ); + }, + function () { return $('#example tbody td:eq(5)').html() === "allan"; } + ); + + oTest.fnComplete(); +} ); \ No newline at end of file