2016-04-11 340 views
0

我有一個JSON數據是這樣的如何從JSON響應

var data = '{"aaData":[{"rrno":"RR201600001","name":"Vikram","dob":"2016-04-11","gender":"Male","job_profile":"Buisness Associate","graduation":"B.Tech\/B.E.","total_exp":1,"status":"Accepted"}, 
{"rrno":"RR201600002","name":"Rahul","dob":"1992-10-13","gender":"Male","job_profile":"Buisness Associate","graduation":"B.Tech\/B.E.","total_exp":3,"status":"Rejected"}]}'; 

然後我加載這個數據在數據表中這樣

var table = $('#dataTable1').DataTable(); 
     table.ajax.url(url).load(); 

加載一個jQuery數據表HTML是這樣的

<table id="dataTable1" class="table table-bordered table-striped-col"> 
    <thead> 
    <tr> 
     <th>Sourcing ID</th> 
     <th>Name</th> 
     <th>Dob</th> 
     <th>Gender</th> 
     <th>Job Profile</th> 
     <th>Basic/Graduation</th> 
     <th>Total Experience</th> 
     <th>Final Status</th> 
    </tr> 
    </thead> 
</table> 

但我收到一個POPUP作爲警報說

DataTables warning: table id=dataTable1 - Requested unknown parameter '0' for row 0

enter image description here

+0

你有一個字符串,而不是一個對象。嘗試刪除開始和結束的'''。 – annoyingmouse

回答

0

你可以嘗試這樣的:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#dataTable1').dataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": { 
      'url':url, 
      "dataSrc": "Responses" 
     } 
    }); 
}); 

+0

它沒有工作,我只是看到處理字在表上,沒有什麼是加載和ALSO'未捕獲TypeError:無法讀取控制檯中的未定義錯誤的屬性'長度' – Vikram

+0

我認爲你的JSON格式不正確。確保它的格式正確。 – Sumanta736

+0

我發佈了Json並檢查了它是否有效,並且它顯示正確使用此鏈接http://jsonlint.com/ – Vikram

0

你好,你可以這樣寫

var dataSet = [ 
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ], 
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ], 
[ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ]]; 

$(document).ready(function() { 
$('#example').DataTable({ 
    data: data , 
    columns: [ 
     { title: "Name" }, 
     { title: "Position" }, 
     { title: "Office" }, 
     { title: "Extn." }, 
     { title: "Start date" }, 
     { title: "Salary" } 
    ] 
}); 

});

更多的參考請看這個鏈接:https://datatables.net/examples/data_sources/js_array.html

+0

但如何使用Ajajx自動加載數據,我有一個大表,它將繪畫將每個轉換爲數組 – Vikram

+0

您可以使用Ajax源數據通過調用數據表中的動作從表中加載您的大數據我認爲這個鏈接將有助於:https://datatables.net/examples/data_sources/ajax.html –