jQuery Datatables处理Null子对象

时间:2015-08-07 14:54:58

标签: javascript jquery json datatables

我正在使用jQuery DataTables,但在我的JSON子对象为null时遇到问题。我的JSON有一个嵌套对象address,可以是null,因此下面的address.streetAddress1会返回错误/警告。

  

DataTables警告:table id = directory-table - 第0行请求的未知参数'address.streetAddress1'。有关此错误的详细信息,请参阅http://datatables.net/tn/4

有没有办法处理null值,所以它只是一个空字符串?

$(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"}
        ]
    } );
} );

此记录nulladdress

{"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"}

此记录包含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":"monica@gmail.com","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"}

2 个答案:

答案 0 :(得分:6)

使用columns.defaultContent选项。

根据manual

  

此外,此选项在加载JSON数据时非常有用,因为如果发现JSON中的单元格值为null,则将使用此处设置的值(例如,您可以设置默认字符串“不可用。”)。

例如:

        {"data" : "address.streetAddress1", "defaultContent": ""},
        {"data" : "address.city", "defaultContent": ""},
        {"data" : "address.state", "defaultContent": ""},
        {"data" : "address.zipCode", "defaultContent": ""},

答案 1 :(得分:1)

使用aoColumns,您可以指定mRender参数函数调用,以检查相关值是否为null。例如:

$(document).ready(function() {
  $('#directory-table').dataTable( {
    "lengthMenu": [ 10, 25, 50, 100 ],
    "dom": 'C<"clear">lfrtip',
    "ajax": {
        "url": "directory.json",
        "dataSrc" : ""
    },
    scrollX:true,
    "aoColumns": [
        {"mData" : "fileId",
         "mRender": function( data, type, full) {
           if( data !== null ){
             return data;
           }
           else{
             return "";
           }
         },
         //More column definitions

http://legacy.datatables.net/usage/columns