我正在尝试填充dataTable,如下所示:
$("#my-datatable").dataTable( {
"sAjaxSource" : "/someURLOnMyServer",
"bDestroy" : true,
"fnServerParams" : function(serverParams) {
serverParams.push(
{
"name" : "widget",
"value" : token
}
);
}
});
它填充的HTML表格:
<table id="my-datatable">
<thead>
<tr>
<th>Type</th>
<th>Value</th>
<th>ID</th>
<th>Fizz</th>
<th>Buzz</th>
</tr>
</thead>
<tbody></tbody>
</table>
根据Firebug,从服务器返回的JSON是:
[
{
"id":1,
"attributeType":{
"id":1,
"name":"test1",
"tag":"test-type",
"is-dog":false
},
"attributeValue":{
"id":null,
"name":"blah",
"tag":"BLAH"
},
"buzz":1,
"fizz":"53abc"
}
]
但Firebug在其控制台中抛出以下JavaScript错误:
TypeError: aData is undefined
[Break On This Error]
for ( i=0 ; i<aData.length ; i++ ) --> jquery.dataTables.js (line 2541)
有人能发现出了什么问题吗?要么我没有正确设置我的dataTable
对象,要么返回的JSON与它试图填充的HTML表的“模式”不匹配。无论哪种方式,我都输了。提前谢谢!
答案 0 :(得分:9)
数据表需要特定的结果格式。如果您不使用该格式,则必须声明所有内容。
$('#my-datatable').dataTable( {
"sAjaxSource": "/url/here",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "widget", "value": "token" } );
request = $.ajax({
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": fnCallback
});
},
"aoColumns": [
{ "mDataProp": "id"},
{ "mDataProp": "fizz"},
{ "mDataProp": "name"},
{ "mDataProp": "tag"},
{ "mDataProp": "tag"},
{ "mDataProp": "attributeValue.name"},
{ "mDataProp": "attributeValue.tag"},
],
});
格式为:http://datatables.net/release-datatables/examples/server_side/post.html
答案 1 :(得分:1)
尝试用aaData
封装您的JSON对象,如:
{"aaData" :
[{"id":1,"attributeType":{"id":1,"name":"test1","tag":"test-type","is-dog":false},"attributeValue":{"id":null,"name":"blah","tag":"BLAH"},"buzz":1,"fizz":"53abc"}]
}