【发布时间】:2019-01-28 12:24:08
【问题描述】:
尝试使用 ajax 填充数据表时出现以下错误:DataTables 警告:表 id=employeeTable - 请求第 0 行第 0 列的未知参数“0”。有关此错误的更多信息,请参阅@987654321 @。 该表实际上具有正确的行数,但所有行都是空的。输出行如下所示:
<tr role="row" class="odd"><td class="sorting_1"></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr role="row" class="even"><td class="sorting_1"></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
这个问题有很多答案,但似乎没有一个能解决我的问题。我使用的代码是:
function fillEmployeeTable(store_nr,week_limit) {
$('#employeeTable').DataTable( {
"ajax": {
"method": "GET",
"url": "api/employeetable/data",
"data": {
"store_nr": store_nr,
"week_limit": week_limit,
},
"columns": [
{ "data": "function_name" },
{ "data": "register_id" },
{ "data": "age" },
{ "data": "contract_until" },
{ "data": "worked_hours" },
{ "data": "days" },
{ "data": "costs" },
{ "data": "hourly_rate" },
{ "data": "contract_from" }
]
}
});
}
使用以下 HTML
<table class="table" id="employeeTable">
<thead>
<tr>
<th>function_name</th>
<th>register_id</th>
<th>age</th>
<th>contract_until</th>
<th >worked_hours</th>
<th>days</th>
<th>costs</th>
<th>hourly_rate</th>
<th>contract_from</th>
</tr>
</thead>
</table>
</div>
(document).ready(function() {
fillEmployeeTable({{default}})
});
而返回的JSON数据为:
{"data": [{"function_name": "Y", "register_id": "1", "age": 26, "contract_from": "01-07-18", "contract_until": "31-12-99", "worked_hours": 1, "days": 9, "costs": 7, "hourly_rate": 2}, {"function_name": "X", "register_id": "1", "age": 18, "contract_from": "24-01-18", "contract_until": "31-07-18", "worked_hours": 486.25, "days": 76, "costs": 1, "hourly_rate": 2}]}
有人知道我该如何解决这个问题吗?
【问题讨论】:
-
我认为您缺少表格的主体
-
在我看到的所有示例中,当您检查页面源时,您也没有在 HTML 中添加 tbody,您会看到 tbody 是由 javascript 代码添加的。
标签: jquery datatables