2016-04-11 488 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 –