2015-05-26 59 views
1

到目前爲止,我已經能夠使用datatables而不會出現從json中顯示ajax數據的問題。 但今天我遇到了這個問題。隨着具有以下結構的JSON:使用json(ajax)在新行中創建Datatables對象

{ 
    "aaData": [ 
     { 
      "id": 22, 
      "name": "flavor 22", 
      "flavorItem": [ 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       }, 
       { 
        "hostSuffix": "google" 
       }, 
       { 
        "hostSuffix": "yahoo" 
       } 
      ] 
     } 
    ] 
} 

當加載到數據表這說明我在同一行中的所有對象「hostSuffix」。 如何爲每個對象顯示新行?

我的腳本:

$("#example").dataTable({ 
    "serverSide": true, 
    "searching": false, 
    "aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]], 
    "iDisplayLength": 10, 
    "ajax": "<c:url value='/ajax/selectflavorEditor?id=22'/>", 
    "columns": [ 
     {"data": "flavorItem[, ].hostSuffix"} 

    ] 
}); 

回答

0

您必須設置dataSrc在嵌套陣列flavorItem點:

$("#example").dataTable({ 
    "serverSide": true, 
    "searching": false, 
    "aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]], 
    "iDisplayLength": 10, 
    "ajax": { 
     url: "<c:url value='/ajax/selectflavorEditor?id=22'/>", 
     dataSrc: function(json) { 
      return json['aaData'][0].flavorItem 
     } 
    }, 
    "columns": [ 
     {"data": "hostSuffix"} 
    ] 
}); 
+0

非常感謝您! –