Problem with null JSON field values

Problem with null JSON field values

jborrajojborrajo Posts: 14Questions: 0Answers: 0
edited September 2009 in Bug reports
Hi,

Another fix I did to get the plugin to work with server side processing, ASP.NET MVC with C#, POSTing with $.ajax as described in another discussion.

If the JSON data includes objects with null fields, then function _fnAddData fails reporting a null.

I edited function _fnAddData like this to handle JSON objets with null fields:

[code]
/* Create the cells */
var nTd;
for (var i = 0; i < aData.length; i++) {
if (!aData[i]) aData[i] = '';
...
[/code]

HTH, regards

Javier

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Hi Javier,

    I think that this is actually a fairly dangerous change to make - DataTables objects to null data, which I think is quite reasonable since it's not really data at all, but if you modify the return from the server-side it could lead to all sorts of issues, including hiding bugs in the server-side code where it is returning null where it shouldn't be. Can you not just have your server-side processing output a blank string rather than null?

    Regards,
    Allan
  • jborrajojborrajo Posts: 14Questions: 0Answers: 0
    Hi Allan

    OK I modified the server side code to convert nulls into string.Empty

    I also fixed the typo (!!) on the $.ajax code, so now I can use the plugin with no custom changes at all.

    Nice!

    About the null values: the plugin fails in an ugly way when dealing with null vales. I think some kind of:
    detect null + report null is a problem + not fail rendering the table
    would be better.

    Something like:

    [code]
    /* Create the cells */
    var nTd;
    for (var i = 0; i < aData.length; i++) {
    if (!aData[i]) aData[i] = '**NULL**'; // an alert would be too much
    ...
    [/code]

    Best regards

    Javier
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Hi Javier,

    In general I agree - an error message + handling the error would be quite a nice thing to do. However, at the moment there is no trace debug messages in DataTables - this is something I've been thinking about for the next major release. And the other thing that I'd be very concerned about would be that it would hide errors which should be found and fixed. As such, I don't think I'll include this in the 1.5.x series, but I've got it on my to-do list to look at possibly implementing this for the next major version.

    Regards,
    Allan
This discussion has been closed.