Handling empty objects in cell data
Handling empty objects in cell data
deepakalur
Posts: 2Questions: 0Answers: 0
I am using a webservice that returns JSON data, so I have no control over how it returns this data. However, in some cases, the cells returned contain empty objects (i.e. { } ) instead "null". In this case, DataTables displays "[object Object]" in the cell which is unsightly and misleading to the end users.
I am wondering if there is a fix for this already. If not, I found a way to fix it by adding these lines to the function_fnGetCellData() as follows:
[code]
if ((sData != null) && (typeof sData === 'object')) {
if (jQuery.isEmptyObject(sData)) {
return ((oCol.sDefaultContent)?oCol.sDefaultContent:'');
}
}[/code]
thoughts?
I am wondering if there is a fix for this already. If not, I found a way to fix it by adding these lines to the function_fnGetCellData() as follows:
[code]
if ((sData != null) && (typeof sData === 'object')) {
if (jQuery.isEmptyObject(sData)) {
return ((oCol.sDefaultContent)?oCol.sDefaultContent:'');
}
}[/code]
thoughts?
This discussion has been closed.
Replies
I'd suggest using mRender for this:
[code]
mData: "myDataProp",
mRender: function ( data, type, row ) {
return $.isEmptyObject( row ) ?
'' :
data;
}
[/code]
$.isEmptyObject is slow, so I'm very reluctant to include that in DataTables core.
Allan
Here is the test case (Please see the sample JSON data here: https://gist.github.com/deepakalur/5264459).
If you notice the first record in the array, i.e. records.record[0] you will see certain elements coming back as empty {}. E.g. the senate_class, eventful_id, nickname, and so on.
[code]
...
"chamber": "house",
"senate_class": {
},
"middlename": "L.",
"fax": "202-225-1589",
"eventful_id": {
},
"website": "http:\/\/ackerman.house.gov\/",
"nickname": {
},
"votesmart_id": "26970",
...
[/code]
I hope this helps.
Allan