I am using jQuery DataTables but encountering issues when my JSON sub object is null
. My JSON has a nested object address
which can be null
, thus address.streetAddress1
below returns an error/warning.
DataTables warning: table id=directory-table - Requested unknown parameter 'address.streetAddress1' for row 0. For more information about this error, please see http://datatables.net/tn/4
Is there a way to handle null
values so it is just a blank string?
$(document).ready(function() {
$('#directory-table').dataTable( {
"lengthMenu": [ 10, 25, 50, 100 ],
"dom": 'C<"clear">lfrtip',
"ajax": {
"url": "directory.json",
"dataSrc" : ""
},
scrollX:true,
"columns": [
{"data" : "fileId"},
{"data" : "fileName"},
{"data" : "institutionId"},
{"data" : "id"},
{"data" : "firstName"},
{"data" : "middleName"},
{"data" : "lastName"},
{"data" : "prefix"},
{"data" : "suffix"},
{"data" : "preferredName"},
{"data" : "gender"},
{"data" : "deleteFlag"},
{"data" : "campusId"},
{"data" : "buildingId"},
{"data" : "primaryEmail"},
{"data" : "secondaryEmail"},
{"data" : "primaryPhone"},
{"data" : "secondaryPhone"},
{"data" : "address.streetAddress1"},
{"data" : "address.city"},
{"data" : "address.state"},
{"data" : "address.zipCode"},
{"data" : "orcid"},
{"data" : "trResearcherId"},
{"data" : "status.name"},
{"data" : "roomNumber"},
{"data" : "createdAt"},
{"data" : "updatedAt"}
]
} );
} );
This record has null
for address
:
{"fileId":2,"fileName":"9999_DIRECTORY.csv","institutionId":1,"id":"EVER1003","firstName":"George","lastName":"Clooney","middleName":null,"prefix":null,"suffix":null,"preferredName":null,"gender":"M","deleteFlag":"N","campusId":null,"primaryEmail":"cclooney","secondaryEmail":null,"primaryPhone":null,"secondaryPhone":null,"address":null,"buildingId":null,"orcid":null,"trResearcherId":null,"status":{"id":0,"name":"Processed"},"tags":null,"roomNumber":null,"createdAt":"2015-07-22 15:41 PM GMT","updatedAt":"2015-07-22 15:41 PM GMT"}
This record has an address
:
{"fileId":2,"fileName":"9999_DIRECTORY.csv","institutionId":1,"id":"EVER1013","firstName":"Monica","lastName":"Galler","middleName":null,"prefix":null,"suffix":null,"preferredName":null,"gender":"F","deleteFlag":"N","campusId":"CAMP1000","primaryEmail":"[email protected]","secondaryEmail":null,"primaryPhone":null,"secondaryPhone":null,"address":{"id":0,"streetAddress1":"123 Fake Street","streetAddress2":null,"city":"Cincinnati","state":"OH","stateId":0,"zipCode":"32444"},"buildingId":null,"orcid":null,"trResearcherId":null,"status":{"id":0,"name":"Processed"},"tags":null,"roomNumber":null,"createdAt":"2015-07-23 14:31 PM GMT","updatedAt":"2015-07-24 18:18 PM GMT"}
Nested object data (objects) DataTables has the ability to use data from almost any JSON data source through the use of the columns.data option. In its simplest case, it can be used to read arbitrary object properties, but can also be extended to n levels of nested objects / arrays through the use of standard Javascript dotted object notation.
Nested object data (objects) DataTables has the ability to use data from almost any JSON data source through the use of the columns.data option.
In my case, even though SQL Server (glad I don't have to use Oracle anymore) allows null values, as you know, submitting an empty string into this nullable field will cause SQL Server to store a value of 1900-01-01 00:00:00.000 into the field.
The common features of DataTables are sorting, ordering, searching, and pagination. DataTables can easily read information for the columns from any nested JSON data source or arrays.
Use columns.defaultContent
option.
According to the manual:
Additionally, this option can be useful when loading JSON data, as the value set here will be used if the cell value from the JSON is found to be
null
(for example, you might set a default string of "Not available.").
For example:
{"data" : "address.streetAddress1", "defaultContent": ""},
{"data" : "address.city", "defaultContent": ""},
{"data" : "address.state", "defaultContent": ""},
{"data" : "address.zipCode", "defaultContent": ""},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With