1. 程式人生 > >datatables 從巢狀的json資料獲取表格資料列

datatables 從巢狀的json資料獲取表格資料列

Ajax---------------
從巢狀的json資料獲取表格資料列
$(document).ready(function() {
    $('#example').dataTable( {
        "processing": true,
        "ajax": "data/objects_deep.txt",
        "columns": [
            { "data": "name" },
            { "data": "hr.position" },
            { "data": "contact.0" },
            { "data": "contact.1" },
            { "data": "hr.start_date" },
            { "data": "hr.salary" }
        ]
    } );
} );
------------------------------
{
  "data": [
    {
      "name": "Tiger Nixon",
      "hr": {
        "position": "System Architect",
        "salary": "$320,800",
        "start_date": "2011/04/25"
      },
      "contact": [
        "Edinburgh",
        "5421"
      ]
    },
    {
      "name": "Garrett Winters",
      "hr": {
        "position": "Accountant",
        "salary": "$170,750",
        "start_date": "2011/07/25"
      },
      "contact": [
        "Tokyo",
        "8422"
      ]
    }]
 }



 從巢狀的陣列獲取資料
 $(document).ready(function() {
    $('#example').dataTable( {
        "ajax": "data/objects_subarrays.txt",
        "columns": [
            { "data": "name[, ]" },
            { "data": "hr.0" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "hr.2" },
            { "data": "hr.1" }
        ]
    } );
} );

 {
    "name": [
        "Nixon",
        "Tiger"
    ],
    "hr": [
        "System Architect",
        "$3,120",
        "2011/04/25"
    ],
    "office": "Edinburgh",
    "extn": "5421"
}
//Name列是從一個包含兩個元素的name數組裡獲取的,展示時陣列中的元素會自動被串聯起來,name[,]括號裡面的分隔符會被用做連線陣列元素的連線符。
//下面的hr.1 hr.2直接通過陣列下標獲取單獨的陣列元素。