我正在尝试使用 jquery datatables插件通过从 PHP 脚本返回的 JSON 对象填充 html 表。我对 json 对象的命名和数据表插件中的选项有一些困惑。像这样传回 json 对象:
echo json_encode(array('data'=>$res));
产量:
Uncaught TypeError: Cannot read property 'length' of undefined
将数据更改为aaData
时成功填充表格,但没有标题。
几个论坛帖子提到 json 对象必须作为 aaData 返回,但其网站上的数据表示例文档给出了 example以数据命名的 json 对象。
这是我当前的客户端代码,我相对确定我的服务器端是正确的。
$('document').ready(function(){
$('#result').dataTable({
"ajax": {
'url' : 'ppmAlarms.php',
},
"columns": [
{ "data": "location" },
{"data" : "descAndType" },
{"data" : "timestamp"}
]
});
});
JSON结构如下:
{
"aaData": [
{
"location": "loc1",
"descAndType": "desc1",
"timestamp": "2014-08-20 11:34:10"
}
]
}
最佳答案
刚刚意识到我只需要在 thead 中指定我自己的列标题,它们不会被填充:
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
关于javascript - jquery 数据表包含空白表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433012/