2016-06-29 23 views
0

DataTables Cannot read property 'length' of undefined排序不工作的數據表插件

我有和上面的鏈接相同的問題。我有一個鏈接到JSON數據。

我JQUERY:

$('#card-table').DataTable({ "ajax": "http://project-maria-1350.appspot.com/getPeople?key=CX1231466035200" });

我的HTML代碼:

`<div id="passenger" class="tab-pane fade in active"> 
    <table id="card-table" class="display" cellspacing="0" width="100%"> 
    <thead> 
     <tr> 
      <th>Last Name</th> 
      <th>First Name</th> 
      <th>Gender</th> 
      <th>Nationality</th> 
      <th>Age</th> 
      <th>Class</th> 
      <th>Boarding No.</th> 
      <th>Seat No.</th> 
      <th>Group Code</th> 
     </tr> 
    </thead> 
    </table> 
</div>` 

我越來越Uncaught TypeError: Cannot read property 'length' of undefined顯示.the數據。但排序不起作用。 請幫我。謝謝。

+0

您的後端列數是否與HTML中指定的數量相同?請檢查它,因爲大部分時間都會導致未定義的長度問題。 –

+0

你是否得到這個基於任何特定領域的排序錯誤?根據你的代碼和數據來分析這個運算器https://plnkr.co/edit/9ug2unk2kc4NuGLbvW2v?p=preview,我沒有看到任何問題。 – Deep

+0

不,這根本不是同一個問題。首先你的數據不是JSON,它是一串字符串數組;此外,您的服務器腳本沒有'Access-Control-Allow-Origin',因此在基於瀏覽器的dataTables上下文中無用。 – davidkonrad

回答

0

數據表格預計JSON對象的格式爲{"data": [{..,..},{...,...}]}

選中此示例https://datatables.net/examples/data_sources/ajax.html (單擊「AJAX」選項卡並查看格式)。

您可以更改它。

enter image description here

+0

謝謝。現在沒有錯誤。但是,仍然排序不起作用。我需要改變我的JSON結構。任何想法將我的數據轉換爲JSON? – Johnleo

+0

好的。你必須讓我看看你的後端代碼。它是PHP與SQL? – Fredster

+0

我解決了它。謝謝。 – Johnleo