2016-10-10 66 views
1

我有一個jQuery DataTables不顯示任何結果的問題。我有有效的JSON,並且使用Laravel 5.2和Yajra DataTables軟件包。來自嵌套對象數據的數據源 - 不加載

我不斷收到這樣的信息:

「的DataTable警告:表ID =用戶 - 請求的未知參數 ‘姓’0行,列0。有關這個 錯誤的詳細信息,請參閱http://datatables.net/tn/4

我的代碼是:JS

$('#users').DataTable({ 
    "processing": true, 
    "ajax": "clients/json", 
    "columns": [ 
     { "data": 'firstname' } 
    ] 
}); 

HTML

<table id="users" class="table table-hover table-condensed"> 
<thead> 
 <tr> 
 <th>Firstname</th> 
 </tr> 
 </thead> 
 </table> 

JSON數據如下:

{"draw":0,"recordsTotal":7,"recordsFiltered":7,"data":[{"\"uniqueid\"":"57cea728a724c","\"firstname\"":"ken","\"lastname\"":"ertert      ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"[email protected]","\"tems\"":1,"\"children\"":{"\"3\"":{"\"first_name\"":"tertyreter","\"last_name\"":"etywert      "}}},{"\"uniqueid\"":"57c69f469b1fb","\"firstname\"":"bryan","\"lastname\"":"johnson      ","\"telephone\"":"03454345324","\"mobile\"":"03453523452","\"postcode\"":"sr690k","\"email\"":"[email protected]","\"tems\"":1,"\"children\"":{"\"4\"":{"\"first_name\"":"zak","\"last_name\"":"johnson      "},"\"5\"":{"\"first_name\"":"sue","\"last_name\"":"johnson      "}}},{"\"uniqueid\"":"57cd426ed8414","\"firstname\"":"not","\"lastname\"":"paying      ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"r55rwefe","\"email\"":"[email protected]","\"tems\"":1,"\"children\"":{"\"2\"":{"\"first_name\"":"cant","\"last_name\"":"affordit      "}}},{"\"uniqueid\"":"57ce97a188f6f","\"firstname\"":"barry","\"lastname\"":"sdf       ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"[email protected]","\"tems\"":null,"\"children\"":{"\"1\"":{"\"first_name\"":"wherareU","\"last_name\"":"        "}}},{"\"uniqueid\"":"57f3a5f56539d","\"firstname\"":"bob","\"lastname\"":"smith       ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"[email protected]","\"tems\"":null,"\"children\"":{"\"8\"":{"\"first_name\"":"claire","\"last_name\"":"smith"}}},{"\"uniqueid\"":"57cd410b0a601","\"firstname\"":"keey","\"lastname\"":"smith       ","\"telephone\"":"01928272623","\"mobile\"":"07836736534","\"postcode\"":"ts34 y78","\"email\"":"[email protected]","\"tems\"":1,"\"children\"":{"\"7\"":{"\"first_name\"":"blake","\"last_name\"":"smith       "}}},{"\"uniqueid\"":"57d29c2f72883","\"firstname\"":"test","\"lastname\"":"waiti       ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"[email protected]","\"tems\"":null,"\"children\"":{"\"6\"":{"\"first_name\"":"dunni","\"last_name\"":"sdf       "}}}],"input":{"_":"1476088895989"}} 

回答

1

出於某種原因,你的JSON字段名稱包含雙引號。另外,您正在使用Yajra DataTables的服務器端處理模式,但在客戶端缺少serverSide: true選項。

匹配您的數據的代碼應該是:

$("#users").DataTable({ 
    "processing": true, 
    "serverSide": true, 
    "ajax": "clients/json", 
    "columns": [ 
     { "data": '"firstname"' } 
    ] 
}); 

但是在某處你的PHP代碼,你設置有額外的雙引號的列名,它應該是簡單的{ "data": "firstname" }上,如果你的客戶端可以找到並刪除多餘的雙引號。