2017-04-21 114 views
0

我使用jQuery的數據表在我的應用程序,插件鏈接https://datatables.net/如何用json填充jquery數據表?

我想填充我的JSON數據表,但我failed.here是我的代碼。

HTML:

<table id="example" class="display" width="100%" cellspacing="0"> 
          <thead> 
          <tr> 
           <th>id</th> 
           <th>Name</th> 
           <th>Code</th> 
           <th>Description</th> 
           <th>isActive</th> 
          </tr> 
          </thead> 
          <tfoot> 
          <tr> 
           <th>id</th> 
           <th>Name</th> 
           <th>Code</th> 
           <th>Description</th> 
           <th>isActive</th> 
          </tr> 
          </tfoot> 
          <tbody> 
          </tbody> 
         </table> 

JS:

$(document).ready(function() { 
    console.log("hi ready"); 
    $('#example').DataTable({ 

     retrieve: true, 
     ajax: { 

      url: '/ProductLicensingApplication/feature/list', 
      dataSrc: '' 
     }, 

     "columns": [ 
      { "data": "id" }, 
      { "data": "name" }, 
      { "data": "code" }, 
      { "data": "description" }, 
      { "data": "isActive" } 
     ] 
    }); 
}); 

的json json data

,但我不能來填充數據插入表作爲表顯示沒有可用數據桌子..你可以看到圖像

enter image description here 請告訴我在我的代碼中有什麼問題...

+0

你的控制檯日誌中是否有錯誤信息?在這個例子中,你可以找到[這裏](https://datatables.net/examples/data_sources/ajax.html),他們使用與你一樣的方法,但是他們沒有在javascript中定義列。只在html中。您的''也應該刪除,或者至少在''之前。由於示例中不包含'',我想你可以將其刪除。 –

+0

不,我沒有得到控制檯中的任何錯誤,我已經看到了這個例子,但它的Ajax數據是從我的JSON數據不同 – Ahmad

回答

1

正如documentation寫的那樣。 ajax.dataSrc選項用於告訴DataTables數據數組處於JSON結構中。一個空字符串是一個特殊情況,它告訴DataTables期待一個數組。

在你的情況JSON是一個對象,你應該設置dataSrc : 'features'

+0

謝謝你的工作... – Ahmad

1

艾哈邁德,

任一組dataSrc : 'features'或者如果可能的響應數據重命名的屬性名稱「功能」到「數據」。

+0

謝謝... upvoted – Ahmad