当我尝试将DataTable连接到WebApi控制器时,我收到此错误:
"DataTables warning: table id=mytable - Requested unknown parameter '0' for row 0."
当我在WebApi控制器中查看响应时:
{"sEcho":"1","iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[{"id":2,"name":"Name2","description":"Description2"},{"id":4,"name":"Name2","description":"Description2"}]}
我的DataTable代码如下所示:
$(function () {
$('#mytable').dataTable({
"bServerSide": true,
"bProcessing": true,
"sAjaxSource": "api/getmystuff",
"sAjaxDataProp": "aaData",
"sPaginationType": "full_numbers",
"Columns": [
{ "mData": "Id" },
{ "mData": "Name" },
{ "mData": "Description" }
]
});
});
这是我的表格标记:
<table id="materials" class="grid">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody></tbody>
</table>
我的json响应有问题吗?或者我如何实现dataTable?我查看了几个例子,但我没有看到任何明显的例子。
答案 0 :(得分:0)
这里的问题是我如何定义dataTable javascript中的列。而不是使用:
"Columns": [
{ "mData": "Id" },
{ "mData": "Name" },
{ "mData": "Description" }
]
我应该使用:
"aoColumns": [
{ mData: "Id" },
{ mData: "Name" },
{ mData: "Description" }
]
请注意,mData和Columns周围的缺失引号已更改为aoColumns