2016-03-25 80 views
0

我正在嘗試從動態數據進行分頁。我開始使用Datatable,但我無法使用JSON對象加載Datatable。 請在下面找到我的代碼:無法使用JSON對象加載Datatable

function drawTable(data) { 

    $('#t01').DataTable({ 
     ajax: { 
     "aaData": data, 
     "dataSrc": "" 
     }, 
     "aoColumns": [ 
      { "mdata": "UserName" }, 
      { "mdata": "AppName" }, 
      { "mdata": "OS" }, 
      { "mdata": "Changes" }, 
      { "mdata": "Time" } 
     ] 
    }); 
} 

我的JSON:

[{ 
    "UserName": "testUser", 
    "AppName": "mtv_app", 
    "OS": "android", 
    "Changes": "tveEnabled : true to false, ", 
    "Time": "2016-03-22 11:36:09" 
}, { 
    "UserName": "testUser", 
    "AppName": "mtv_app", 
    "OS": "android", 
    "Changes": "tveEnabled : false to true, ", 
    "Time": "2016-03-22 11:44:11" 
},.. 

我的HTML頁面:

<table id="t01" class="display" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>UserName</th> 
       <th>AppName</th> 
       <th>OS</th> 
       <th>Changes</th> 
       <th>Time</th> 

      </tr> 
     </thead> 
    </table> 

表中,無法加載,但加載...出現在表中。我檢查過,JSON格式正確。

我修改了代碼:

function drawTable(data) { 
    console.log(data); 
    $('#t01').DataTable({ 
     "processing" : true, 
     "data": data, 
     "columns": [ 
      { "data": "UserName" }, 
      { "data": "AppName" }, 
      { "data": "OS" }, 
      { "data": "Changes" }, 
      { "data": "Time" } 
     ] 
    }); 
} 

,現在我的正裝表與空白列,我得到的錯誤爲: 數據表警告:表ID = T01 - 請求的未知參數「用戶名」 0行,列0

+0

查看瀏覽器控制檯是否有任何錯誤。 –

+0

@KartikeyaKhosla:我沒有在控制檯中得到任何錯誤:( –

+0

刪除並嘗試''aoColumns「:'設置 – Ashan

回答

0

您的代碼工作對我罰款。這是我如何使用它: 第一:我創建了一個json.php誰包含以下代碼:

[{ 
    "UserName": "testUser", 
    "AppName": "mtv_app", 
    "OS": "android", 
    "Changes": "tveEnabled : true to false, ", 
    "Time": "2016-03-22 11:36:09" 
}, { 
    "UserName": "testUser", 
    "AppName": "mtv_app", 
    "OS": "android", 
    "Changes": "tveEnabled : false to true, ", 
    "Time": "2016-03-22 11:44:11" 
}] 

我創建其他頁面test.php後這些下列代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Test</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" /> 
<link rel="stylesheet" type="text/css" href="https://www.datatables.net/release-datatables/extensions/TableTools/css/dataTables.tableTools.css"> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script> 
</head> 
<body> 
<table id="t01" class="display" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>UserName</th> 
       <th>AppName</th> 
       <th>OS</th> 
       <th>Changes</th> 
       <th>Time</th> 

      </tr> 
     </thead> 
    </table> 
</body> 
<script type="text/javascript"> 
function drawTable(data) { 
    console.log(data); 
    $('#t01').DataTable({ 
     "processing" : true, 
     "data": data, 
     "columns": [ 
      { "data": "UserName" }, 
      { "data": "AppName" }, 
      { "data": "OS" }, 
      { "data": "Changes" }, 
      { "data": "Time" } 
     ] 
    }); 
} 
    $(document).ready(function() { 
       $.ajax({ 
        url: "json.php", 
        dataType: "json", 
        success: function(data) { 
         drawTable(data); 
        } 
       }); 
    }); 
</script> 
</html> 

這是結果: enter image description here

+0

我不知道爲什麼,但是當我解析JSON並使用JSON對象作爲輸入(數據在我的函數中),它工作得很好 –

+0

哦,我沒有看到問題出在哪裏,因爲我沒有完整的代碼 –

+0

您沒有將JSON傳遞給函數? –

0

試試這個,我覺得這非常有用

function drawTable(data) { 
     console.log(data); 
     $('#t01').DataTable({ 
      "bProcessing": true, 
      "bServerSide": true, 
      "bFilter" : false, 
      "sAjaxSource": "data.php",//your data source 
      "columns": [ 
       { "data": "UserName" }, 
       { "data": "AppName" }, 
       { "data": "OS" }, 
       { "data": "Changes" }, 
       { "data": "Time" } 
      ] 
     }); 
    }