Default value for nulls
Default value for nulls
As most users have known that if the JSON data contain null and without specifying the "sType" for the column, DataTable's rendering can fail. When we specify a column as "sType = string", each cell with null data will display a "null" string. This is not very good for people who read this.
Is it possible to specify a value like "N/A", number 0, etc., for null data (or empty string by default) in a column?
Using fnRender might help, but it requires specifying the column index (e.g. o.aData[8], o.aData[6], etc) and is somewhat cumbersome for maintenance, when a table has many columns.
[code]
"aoColumns": [
{ "sTitle": "Month", "oDefault": "N/A" },
{ "sTitle": "Text", "sType": "string", "oDefault": "" },
{ "sTitle": "Value", "sType": "numeric", "oDefault": 0 }
]
[/code]
Is it possible to specify a value like "N/A", number 0, etc., for null data (or empty string by default) in a column?
Using fnRender might help, but it requires specifying the column index (e.g. o.aData[8], o.aData[6], etc) and is somewhat cumbersome for maintenance, when a table has many columns.
[code]
"aoColumns": [
{ "sTitle": "Month", "oDefault": "N/A" },
{ "sTitle": "Text", "sType": "string", "oDefault": "" },
{ "sTitle": "Value", "sType": "numeric", "oDefault": 0 }
]
[/code]
This discussion has been closed.
Replies
Regards,
Allan
But it does not support default values mentioned above for null yet.
And the following code no longer works either. I lost control over null values.
[code]
{ "sTitle": "Comment",
"fnRender": function(o) {
return o.aData[6] != null
? o.aData[6]
: "None";
}
}
[/code]
Allan
Thanks for the information. However, it is not what I am talking about.
Please check out this line of code:
[code]
"fnRender": function(o) {
return o.aData[6] != null
? o.aData[6]
: "None";
}
[/code]
It checks aData[6] and see whether it is not null. If the item is not null, then returns it, otherwise, a string "None" instead of null value is returned. The code will never return null.
This line of null value customization code, previously worked on 1.6.2. However, it seems that in 1.7 the engine ignores fnRender when the data item is null.
Regards,
Allan
[code]
"fnRender": function(o) {
return o.aData[6] != null
? o.aData[6]
: "None";
}
[/code]
no longer works in 1.7 beta 3 and should be changed to
[code]
"fnRender": function(o) {
return o.aData[6] != ""
? o.aData[6]
: "None";
}
[/code]
The behavioral change for the NULL values can make previous codes unusable--if the programmers use the above pattern with 1.6.2 to handle NULL values.
However, a zero length string and a null have different meanings in Microsoft SQL Server, if the JSON data source is exported from there. The null is NULL in database, meaning nothing is there. The "" means a string having no character. It could be the case that in the database, NULL means the data has not been specified, but "" means the data has been specified but it is a string with no content. If NULL is changed to "", then the client fnRender function couldn't tell whether the original value in the database is NULL or "".
Could you consider taking my solution in the first post here, letting the DataTable programmer to OPTIONALLY using a specific value for NULL? So, the programmer can have a chance to use fnRender, fnRowCallback, or other callback functions to handle the null value and empty string value separately.
You can comment out line 2419 (look for the comment "Allow null data (from a data array) - simply deal with it as a blank string") which will stop this behaviour. I've put it on my to do list to consider adding an option for this in future.
Regards,
Allan
BTW, it is very weird that Oracle does not distinguish between NULL and a zero length string. :)
Regards,
Allan