Default value for nulls

Default value for nulls

sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
edited July 2010 in General
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]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Try using the 1.7 beta which allows for null "values" in the JSON source, and should take that into account when doing type detection: http://datatables.net/new/1.7 . Let me know if you run into any problems with this, as it should work... :-)

    Regards,
    Allan
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited July 2010
    I've upgraded to 1.7. It does support null values.
    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]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    fnRender must return a string, rather than null or any other primitive - http://datatables.net/usage/columns#fnRender. If you return null, how would you expect DataTables to display it? As an empty string (in which case just return that), or something else. null is the absence of data, which is why DataTables has not supported it before (although I've decided to treat input data as an empty string in 1.7). Hope this helps!

    Allan
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited July 2010
    Hi,

    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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Sorry I misunderstood - however, I believe the answer lies in my previous comments when I say that DataTables will convert null data to an empty string. So what to do with the 1.7 betas is to check from an empty string, rather than checking for null.

    Regards,
    Allan
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited July 2010
    Oh, understood. The previously working code with 1.6.2 like the following
    [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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    A zero length string and null have different meaning in every programming language I can think of - and is partly why DataTables never supported null values - it's impossible to display the absence of data. The fact that it worked for you before is more fluke than intention :-). There were a number of comments in the forum which requested that null be treated as valid in order to allow automatic parsing of JSON, since a lot of parsers put in null values.

    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
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    Thanks for taking it into consideration.
    BTW, it is very weird that Oracle does not distinguish between NULL and a zero length string. :)
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hah! As I was typing that I was thinking "there much be an exception or two". I'm surprised that Oracle doesn't distinguish between the two - but there we go... :-)

    Regards,
    Allan
This discussion has been closed.