Maybe bug and fix: aoColumns Null handling mData and mRender
Maybe bug and fix: aoColumns Null handling mData and mRender
DT: 1.9.3, JQuery: 1.8.2
I've been hunting down a JavaScript error where, when displaying comma-separated values in a column as in the Editor Joined Tables example, I was getting:
[code]"Unable to get value of the property 'length'"[/code] on line 754 of jquery.dataTables.js 1.9.3, which nearby reads:
[code]
// Traverse each entry in the array getting the properties requested
for (var j = 0, jLen = data.length; j < jLen; j++) { // line 754
out.push(fetchData(data[j], type, innerSrc));
}
[/code]
tl;dr: this loop breaks if the object is null.
Specifically, if I used mRender to comma-separate values from the aaData object like so:
[code]
{"mData":"ROTATION"},
{
"mData":"EQUIPMENT",
"mRender":"[, ].name"
},
{"mData":"RECIPE_NOTES"}
[/code]
I would get the error. If I removed the lines for "EQUIPMENT" I'd get no error (and no column).
My aaData looks something like this (generated in C# by MS Web API using JSON.Net):
[code]
{
"id": 1,
"error": "",
"fieldErrors": [],
"data": [],
"aaData": [{
"DT_RowId": "row_2436",
// More columns...
"EQUIPMENT": [
{"id": "4", "name": "foo"},{"id": "5","name": "bar"}, {"id": "7","name": "BOSE speakers suck"}
]},
[/code]
If that EQUIPMENT line is changed to: [code]"EQUIPMENT": null[/code] (which JSON.Net generates when the C# object is null), it breaks with the error above.
I naïvely put "alert(data);" in the loop and saw a lot of pop-ups with the text: [code][object Object],[object Object][/code] which looked fine. I guessed that it might be a null handling problem and found a fix ("hack" is probably a better word):
[code]
Change:
for (var j = 0, jLen = data.length; j < jLen; j++) { ...
To:
if(data) for (var j = 0, jLen = data.length; j < jLen; j++) { ...
[/code]
This gives me correct results in my limited testing even for null values described above.
I've been hunting down a JavaScript error where, when displaying comma-separated values in a column as in the Editor Joined Tables example, I was getting:
[code]"Unable to get value of the property 'length'"[/code] on line 754 of jquery.dataTables.js 1.9.3, which nearby reads:
[code]
// Traverse each entry in the array getting the properties requested
for (var j = 0, jLen = data.length; j < jLen; j++) { // line 754
out.push(fetchData(data[j], type, innerSrc));
}
[/code]
tl;dr: this loop breaks if the object is null.
Specifically, if I used mRender to comma-separate values from the aaData object like so:
[code]
{"mData":"ROTATION"},
{
"mData":"EQUIPMENT",
"mRender":"[, ].name"
},
{"mData":"RECIPE_NOTES"}
[/code]
I would get the error. If I removed the lines for "EQUIPMENT" I'd get no error (and no column).
My aaData looks something like this (generated in C# by MS Web API using JSON.Net):
[code]
{
"id": 1,
"error": "",
"fieldErrors": [],
"data": [],
"aaData": [{
"DT_RowId": "row_2436",
// More columns...
"EQUIPMENT": [
{"id": "4", "name": "foo"},{"id": "5","name": "bar"}, {"id": "7","name": "BOSE speakers suck"}
]},
[/code]
If that EQUIPMENT line is changed to: [code]"EQUIPMENT": null[/code] (which JSON.Net generates when the C# object is null), it breaks with the error above.
I naïvely put "alert(data);" in the loop and saw a lot of pop-ups with the text: [code][object Object],[object Object][/code] which looked fine. I guessed that it might be a null handling problem and found a fix ("hack" is probably a better word):
[code]
Change:
for (var j = 0, jLen = data.length; j < jLen; j++) { ...
To:
if(data) for (var j = 0, jLen = data.length; j < jLen; j++) { ...
[/code]
This gives me correct results in my limited testing even for null values described above.
This discussion has been closed.
Replies
Allan