2014-09-29 39 views
1

由於某種原因,我的數據沒有被填充到表中。無法在jQuery數據表中填充數據

我有一個JSON數據,我只想填充數據表。

有人可以幫助我,讓我知道我錯過了哪一個簡單的步驟?

我有版本1.9.4。

<table id="example"> 
    <thead> 
     <tr><th>name</th> 
      <th>position</th> 
      <th>salary</th> 
      <th>start_date</th> 
      <th>office</th> 
      <th>extn</th>   
    </tr> 
    </thead> 
    <tbody></tbody> 
</table> 

$('#example').dataTable({ 
     data: [ 
      [ 
       "Tiger Nixon", 
       "System Architect", 
       "$3,120", 
       "2011/04/25", 
       "Edinburgh", 
       "5421" 
      ], 
      [ 
       "Garrett Winters", 
       "Director", 
       "5300", 
       "2011/07/25", 
       "Edinburgh", 
       "8422" 
      ] 
     ] 

    }); 

fiddle

回答

3

這裏是一個1.9.4例子。你需要

  1. 通過與對象JSON,而不是一個數組
  2. 目標aaData,不dataaoData
  3. 指定aoColumns,如列 - > JSON值

我在這裏僅使用上述數據的前兩個「列」:

var json = [ 
    { "name" : "Tiger Nixon", "position" : "System Architect" /*,.,.,.*/ }, 
    { "name" : "Garrett Winters", "position" : "Director" /*,.,.,.*/ } 
]; 

var table = $('#example').dataTable({ 
    aaData : json, 
    aoColumns: [ 
     { mDataProp: "name" }, 
     { mDataProp: "position" } 
    ] 
}); 

小提琴 - >http://jsfiddle.net/4e7myzmm/
同一數據表中1.10.x - >http://jsfiddle.net/c27jj9he/

1

相反的data:,你需要使用aaData:

$('#example').dataTable({ 
    aaData: [ 
     [... 

默認情況下數據表將使用返回數據的「aaData」屬性,它是陣列的一個數組表中每列的條目。見docs

jsfiddle