2017-10-11 130 views
0

我無法在DataTable中顯示來自Django Rest Framework的json有效內容,我不知道爲什麼。我已經覆蓋了序列化器來更好地格式化JSON有效載荷,但它仍然無法工作。DataTables Django Rest框架JSON

我可以驗證JSON請求被解僱,並得到適當的信息反饋(可以在瀏覽器的控制檯中看到),但是表說"No data to display"

這裏是我的JSON有效載荷:

{"data":[ 
    {"product_name":"123", 
    "mdr_class":1, 
    "category":1, 
    "eo_role":1, 
    "division":1, 
    "created_by":"root"} 
    {"product_name":"1234", 
    "mdr_class":1, 
    "category":1, 
    "eo_role":1, 
    "division":1, 
    "created_by":"root"} 
    ... 
    ]} 

單線的JSON

{"data":[{"product_name":"123","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"1234","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"234234","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"432","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"432134","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"432134","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"},{"product_name":"432134","mdr_class":1,"category":1,"eo_role":1,"division":1,"created_by":"root"}]} 

,並概述了AJAX調用我的HTML文件。

<table id="example" class="display"> 
<thead> 
    <tr> 
     <th>Product Name</th> 
     <th>MDR Class</th> 
     <th>Category</th> 
     <th>EO Role</th> 
     <th>Division</th> 
     <th>Created By</th> 
    </tr> 
<thead> 
<tbody> 
    <tr> 
     <td>1</td> 
     <td>2</td> 
     <td>3</td> 
     <td>4</td> 
     <td>5</td> 
     <td>6</td> 
    </tr> 
</tbody> 
<tfoot> 
    <tr> 
     <th>Product Name</th> 
     <th>MDR Class</th> 
     <th>Category</th> 
     <th>EO Role</th> 
     <th>Division</th> 
     <th>Created By</th> 
    </tr> 
<tfoot> 
</table> 

<script> 
$(document).ready(function() { 
    $('#example').DataTable({ 

     ajax: { 
     "processing":true, 
     url: 'http://localhost:8000/api/entry/?format=json', 
     "dataSrc":"", 
     }, 


     columns: [ 
      {"mData":"product_name"}, 
      {"mData":"mdr_class"}, 
      {"mData":"category"}, 
      {"mData":"eo_role"}, 
      {"mData":"division"}, 
      {"mData":"created_by"}, 
      ] 
     }); 


}); 

</script> 

再次 - 我可以看到在控制檯中的JSON,但數據不顯示。 我是否需要修改json如何呈現給DataTable?我很困擾。

回答

0

我已經想通了。 該腳本格式錯誤。 這是工作腳本

$(document).ready(function() { 
    $('#example').dataTable({ 

     ajax: { 
     url: 'http://localhost:8000/api/entry/?format=json', 
     }, 


     columns: [ 
      {data:"product_name"}, 
      {data:"mdr_class"}, 
      {data:"category"}, 
      {data:"eo_role"}, 
      {data:"division"}, 
      {data:"created_by"}, 
      ] 
     }); 


});