Car*_*rlM 3 javascript ajax datatable jquery json
我使用DataTables与Jquery,我有一个JSON我想通过获取对象数据源Ajax在表中和显示.
JSON数据从/live/log网址返回,格式如下:
{
"Logs": [
{
"date": "2015-04-22T14:00:39.086Z",
"eventType": "event1",
"str": "Application startup"
},
{
"date": "2015-04-22T14:01:27.839Z",
"eventType": "event2",
"str": "test Logged in"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的表格HTML:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Date</th>
<th>Event</th>
<th>Description</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Date</th>
<th>Event</th>
<th>Description</th>
</tr>
</tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)
最后JS用于获取和填充数据表:
$(document).ready(function() {
$('#example').dataTable( {
"ajax": "/live/log",
"columns": [
{"data": "date"},
{"data": "eventType"},
{"data": "str"}
]
});
});
Run Code Online (Sandbox Code Playgroud)
我可以通过调试器看到JSON数据正在被正确获取.
我似乎在datatables与JSON数据相关的js中出现错误.
Value aData in fnInitalise is null - Uncaught TypeError: Cannot read property 'length' of undefined.数据表陷入困境,说"正在加载......"
我确定这可能是由于我的JSON数据格式化.谁能指出我正确的方向?
您应该访问该Log对象data,因为它是column构建表时将循环的数组.插件的工作原理是假设调用了数组的名称data,即:
{
"data": [
// Array of objects
]
}
Run Code Online (Sandbox Code Playgroud)
但是因为你Log在你的情况下使用:
{
"Logs": [
// Array of objects
]
}
Run Code Online (Sandbox Code Playgroud)
...您需要手动指定dataSrc属性(因为您使用的是自定义数据属性):
$(document).ready(function() {
$('#example').dataTable( {
"ajax": {
"url": "/live/log",
"dataSrc": "Logs"
},
"columns": [
{"data": "date"},
{"data": "eventType"},
{"data": "str"}
]
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2367 次 |
| 最近记录: |