How to prevent null values in aaData during DataTable initialization?

How to prevent null values in aaData during DataTable initialization?

ocramotocramot Posts: 3Questions: 0Answers: 0
edited December 2013 in General
Hello,
I'm receiving some JSON data from an external source, and since I'm trying to optimize the table initialization, I'm using the Dynamic creation example shown in http://datatables.net/release-datatables/examples/data_sources/js_array.html
Unfortunately, it may happen that the JSON object that I receive contains some null values in it, i.e.:

[code]{
"dataArray":[
null,
{
"0ne":"valid JSON object 1",
"Two":123
},
{
"0ne":"valid JSON object 2",
"Two":456
}
],
"other":"data"
}[/code]

My dataTable initiaslization code:
[code]$('#tableName').dataTable( {
"aaData": jsonObject.dataArray,
"aoColumns": [
{ "sTitle": "one", "mData": "One" },
{ "sTitle": "two", "mData": "Two" }
]
} );[/code]

Because of that null value, during initialization I'm getting this alert:

[quote]DataTables warning (table id = 'tableName'): Requested unknown parameter 'One' from the data source for row 0[/quote]

Is there a way to prevent null values during initialization? Or do I have to remove them from my array before initialization? Since I'm trying to optimize, I'd prefer to avoid this last solution.

Thanks for your help and your great work!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I think you'll need to loop over the array you get form the external resource and actually remove the nulls. Although DataTables can handle a null for a specific column, it doesn't do so well with a whole row being null.

    Allan
  • ocramotocramot Posts: 3Questions: 0Answers: 0
    edited December 2013
    Thank for your reply! Currently I have resolved filtering the input before initializing the datatable, thanks to this line of code, found at: http://stackoverflow.com/a/2843625/1876359 :

    [code]json["dataArray"] = json["dataArray"].filter(function(n){return n;});[/code]
This discussion has been closed.