2017-03-26 38 views
-2

我想創建一個JQuery生成的數據源的數據表。JQuery的數據表對象數據源不工作

我有這樣的對象:

[ 
    { 
    "a": -817, 
    "b": null, 
    "c": "Default", 
    "total": -817 
    }, 
    { 
    "a": -21, 
    "b": 12, 
    "c": "Default", 
    "total": -897 
    } 
] 

而這個數據表:

table1.DataTable({ 
    "ajax": dataSource, 
    "columns": [ 
     { 
      "className": 'details-control', 
      "orderable": false, 
      "data": null, 
      "defaultContent": '' 
     }, 
     { 
      "orderable": true, 
      "data": "a", 
      "defaultContent": '' 
     }, 
     { 
      "orderable": true, 
      "data": "b", 
      "defaultContent": '' 
     }, 
     { 
      "orderable": true, 
      "data": "c", 
      "defaultContent": '' 
     }, 
     { 
      "orderable": true, 
      "data": "total", 
      "defaultContent": '' 
     } 
    ], 
    "paging": false, 
    "bInfo": false, 
    "bFilter": false, 
    "order": [ 
     [1, "asc"] 
    ] 
)}; 

我試圖操縱數據源是這樣的:

  • JSON.stringify(數據源)
  • 加數組之前的字段({data:datasource})並更改ajax 「ajax」:{「url」:datasource,「dataSrc」:「data」}有或沒有stringihfy

沒有什麼似乎工作。

請幫助

的問候,伊

回答

1

看來你的數據對象是不是在數據表的正確格式,它應該是這樣的:

{ 
    "data": [ 
    { 
     "name": [ 
     "Nixon", 
     "Tiger" 
     ], 
     "hr": [ 
     "System Architect", 
     "$320,800", 
     "2011/04/25" 
     ], 
     "office": "Edinburgh", 
     "extn": "5421" 
    }] 
    } 

有一個數據對象包裝一切。

看看這個Fiddle和我訪問數據的方式。

+0

謝謝,但它幾乎解決了問題。 我改變你的小提琴模擬我的問題: https://plnkr.co/edit/EmjE11nnx8amkBTiBK2t?p=preview 我必須生成內聯數據(它是一個對象的一部分)。 –

+1

試試這個: https://plnkr.co/edit/YZxiSjslpSOApypi2GUQ?p=preview –

+0

是的......我試圖避免操縱對象並創建這個值的數組。如果沒有其他選擇,我會這樣做。謝謝。 –