sData is null
sData is null
Hi all
I have only just started to use dataTables but I have run into an issue that I can't seem to figure out. I have read through other topics with the same error (in the title of this post), and I can't seem to pin point what my exact problem is.
For some background, I am generating the table dynamically, from json data. Rather than use the ajax capabilities, I have decided to use $.getJSON in jquery to get me the result, then call datatables on this. Reason being that the json returned is from a .NET service so it is formatted in a key:value format. I am using some code on the client to format the json to the format that datatables wants it and I think it is correct. I have run the formatted json through http://www.jsonlint.com/ and it comes up good and I have also checked the format used in the javascript object example on this site.
So some code:
Here is the table definition. As I am regenerating it, I just added a dummy head and body.
[code]
...
...
...
...
[/code]
The code to generate the table:
[code]
var str = JSON.stringify(json);
var data = str.replace(/"+[0-9||a-z||A-Z||_|| ]*"+:+/g, '');
data = data.replace(/{/g, '[');
data = data.replace(/}/g, ']');
var jsonFix = JSON.parse(data);
$("#closestBlocks").dataTable({
"aaData": jsonFix,
"aoColumns": [
/* blk */{"sTitle": "blk" },
/* desc1 */{"bVisible": false },
/* desc2 */{"bVisible": false },
/* manager */{"sTitle": "manager" },
/* name */{"sTitle": "name" },
/* offtakeCompany */{"bVisible": false },
/* project */{"bVisible": false },
/* region */{"bVisible": false },
/* bearing */{"sTitle": "bearing" },
/* direction */{"sTitle": "direction" },
/* distance */{"sTitle": "distance" }
]
}
[/code]
The original json object is as follows (this is copied from firebug, so it is the stringified representation):
[code]
[{"blk":1082,"desc1":null,"desc2":null,"manager":"Midway","name":"McKenna","offtakeCompany":null,"project":null,"region":null,"bearing":19,"direction":"N","distance":2.2},{"blk":1080,"desc1":null,"desc2":null,"manager":"Midway","name":"Stewart","offtakeCompany":null,"project":null,"region":null,"bearing":182,"direction":"N","distance":2.6},{"blk":12803,"desc1":null,"desc2":null,"manager":"EPFL","name":"McKenna D","offtakeCompany":null,"project":null,"region":null,"bearing":51,"direction":"NE","distance":2.8},{"blk":1076,"desc1":null,"desc2":null,"manager":"Midway","name":"Elston","offtakeCompany":null,"project":null,"region":null,"bearing":349,"direction":"N","distance":4.3},{"blk":12802,"desc1":null,"desc2":null,"manager":"EPFL","name":"Hillman","offtakeCompany":null,"project":null,"region":null,"bearing":343,"direction":"N","distance":8.9},{"blk":1088,"desc1":null,"desc2":null,"manager":"Midway","name":"Willis","offtakeCompany":null,"project":null,"region":null,"bearing":234,"direction":"SW","distance":8.9}]
[/code]
Formatted JSON (which is from the data variable that is parsed into the jsonFix object)
[code]
"[[1082,null,null,"Midway","McKenna",null,null,null,19,"N",2.2],[1080,null,null,"Midway","Stewart",null,null,null,182,"N",2.6],[12803,null,null,"EPFL","McKenna D",null,null,null,51,"NE",2.8],[1076,null,null,"Midway","Elston",null,null,null,349,"N",4.3],[12802,null,null,"EPFL","Hillman",null,null,null,343,"N",8.9],[1088,null,null,"Midway","Willis",null,null,null,234,"SW",8.9]]"
[/code]
It has been mentioned in another post on this error that it could be something to do with the datatypes returned. Hopefully I have given enough information here to sort this out. Any ideas?
Any help most appreciated.
James.
I have only just started to use dataTables but I have run into an issue that I can't seem to figure out. I have read through other topics with the same error (in the title of this post), and I can't seem to pin point what my exact problem is.
For some background, I am generating the table dynamically, from json data. Rather than use the ajax capabilities, I have decided to use $.getJSON in jquery to get me the result, then call datatables on this. Reason being that the json returned is from a .NET service so it is formatted in a key:value format. I am using some code on the client to format the json to the format that datatables wants it and I think it is correct. I have run the formatted json through http://www.jsonlint.com/ and it comes up good and I have also checked the format used in the javascript object example on this site.
So some code:
Here is the table definition. As I am regenerating it, I just added a dummy head and body.
[code]
...
...
...
...
[/code]
The code to generate the table:
[code]
var str = JSON.stringify(json);
var data = str.replace(/"+[0-9||a-z||A-Z||_|| ]*"+:+/g, '');
data = data.replace(/{/g, '[');
data = data.replace(/}/g, ']');
var jsonFix = JSON.parse(data);
$("#closestBlocks").dataTable({
"aaData": jsonFix,
"aoColumns": [
/* blk */{"sTitle": "blk" },
/* desc1 */{"bVisible": false },
/* desc2 */{"bVisible": false },
/* manager */{"sTitle": "manager" },
/* name */{"sTitle": "name" },
/* offtakeCompany */{"bVisible": false },
/* project */{"bVisible": false },
/* region */{"bVisible": false },
/* bearing */{"sTitle": "bearing" },
/* direction */{"sTitle": "direction" },
/* distance */{"sTitle": "distance" }
]
}
[/code]
The original json object is as follows (this is copied from firebug, so it is the stringified representation):
[code]
[{"blk":1082,"desc1":null,"desc2":null,"manager":"Midway","name":"McKenna","offtakeCompany":null,"project":null,"region":null,"bearing":19,"direction":"N","distance":2.2},{"blk":1080,"desc1":null,"desc2":null,"manager":"Midway","name":"Stewart","offtakeCompany":null,"project":null,"region":null,"bearing":182,"direction":"N","distance":2.6},{"blk":12803,"desc1":null,"desc2":null,"manager":"EPFL","name":"McKenna D","offtakeCompany":null,"project":null,"region":null,"bearing":51,"direction":"NE","distance":2.8},{"blk":1076,"desc1":null,"desc2":null,"manager":"Midway","name":"Elston","offtakeCompany":null,"project":null,"region":null,"bearing":349,"direction":"N","distance":4.3},{"blk":12802,"desc1":null,"desc2":null,"manager":"EPFL","name":"Hillman","offtakeCompany":null,"project":null,"region":null,"bearing":343,"direction":"N","distance":8.9},{"blk":1088,"desc1":null,"desc2":null,"manager":"Midway","name":"Willis","offtakeCompany":null,"project":null,"region":null,"bearing":234,"direction":"SW","distance":8.9}]
[/code]
Formatted JSON (which is from the data variable that is parsed into the jsonFix object)
[code]
"[[1082,null,null,"Midway","McKenna",null,null,null,19,"N",2.2],[1080,null,null,"Midway","Stewart",null,null,null,182,"N",2.6],[12803,null,null,"EPFL","McKenna D",null,null,null,51,"NE",2.8],[1076,null,null,"Midway","Elston",null,null,null,349,"N",4.3],[12802,null,null,"EPFL","Hillman",null,null,null,343,"N",8.9],[1088,null,null,"Midway","Willis",null,null,null,234,"SW",8.9]]"
[/code]
It has been mentioned in another post on this error that it could be something to do with the datatypes returned. Hopefully I have given enough information here to sort this out. Any ideas?
Any help most appreciated.
James.
This discussion has been closed.
Replies
I guess I could just replace all the null values with empty string values, which hopefully will get it working, but for future reference, is there a way to process these null values in dataTables itself so it doesn't fail?
Thanks for following this up - you are absolutely correct, it's the null values that are causing you problems here. Interestingly this 'null' issue is cropping up a couple of times, so I think I might have to address it, although I don't think it's really right to do so in DataTables...
The following is a repost from my comment at:
http://datatables.net/forums/comments.php?DiscussionID=1051&page=1#Item_2
DataTables doesn't really "like" null data, for the simple fact that null isn't any data at all :-). What is null when you show it in a table? An empty cell possibly, in which case it should be an empty String, or the cell shouldn't exist at all, which would probably break the table altogether.
I don't really think this is a bug in DataTables as such, since, as I say, null is not data, and shouldn't really be in a table. One option might be to replace null with an empty string internally in DataTables when the data is added, but then it's no longer null and I've modified your data, which I'm most reluctant to do... So currently (I'm open to persuasion though!) I think that this should be done externally.
Regards,
Allan
Thanks for the confirmation on what the issue is. I do tend to agree that this is not really a dataTables problem, it is more of an issue with my initial data (which I could solve in the service), or I could modify it on the client, which is what I have ended up doing. Perhaps my only suggestion is that it took me a while to track down what the exact problem actually was, maybe a more informative error message, or perhaps a note in the documentation about dynamically generated data and null values?
Anyhow, thanks very much for the add-in, I feel I will be using it more and more in future work.
James.
I've been thinking about having a 'debug version' of DataTables will can output trace information, such as what it is doing, what data it encounters etc, and this would be a good idea for this kind of thing. This debug idea is starting to gain a bit of traction now - so perhaps in a future release I'll look at including that and building it into the build system.
Regards,
Allan
[code]
Index: ui/jquery.dataTables.js
===================================================================
@@ -672,8 +672,12 @@
function ( sData )
{
/* Snaity check that we are dealing with a string or quick return for a number */
- if ( typeof sData == 'number' )
+ if (sData == null)
{
+ return null;
+ }
+ else if ( typeof sData == 'number' )
+ {
return 'numeric';
}
else if ( typeof sData.charAt != 'function' )
[/code]
Thanks for that - yes that's an interesting change. However, don't really like the idea that null "data" could get there in the first place. DataTables isn't designed to cope with null, since it isn't data, and it's a data display component - I'm also not sure what would happen with null data at other points in DataTables, so I suspect it would just be shunting the problem to somewhere else (filtering/sorting?).
I think the issue of null will need to be addressed at some point - most likely replacing it with an empty string, but I'm not quite sure yet :-)
Regards,
Allan