Handling empty objects in cell data

Handling empty objects in cell data

deepakalurdeepakalur Posts: 2Questions: 0Answers: 0
edited March 2013 in Bug reports
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?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    So the whole row object is empty? Please link to a test case so I can fully understand the problem.

    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
  • deepakalurdeepakalur Posts: 2Questions: 0Answers: 0
    edited March 2013
    Thanks for the tip on using myDataProp. I will use that approach for my fix instead. I understand the reluctance of using isEmptyObject.

    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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I see - if they were to have sub-properties, then you'd be able to use sDefaultContent , but here you probably do need a check for an empty object. Bit unusual to have the lack of data represented by an empty object rather than null or undefined, when the value would be a string, but it is possible to support it :-)

    Allan
This discussion has been closed.