我正在尝试使用 DataTable 创建一个表,但很难让 DataTable 使用 JSON 对象加载。
function getData() {
var request = new XMLHttpRequest();
var json = "link-to-my-json-object";
// Get JSON file
request.onload = function() {
if ( request.readyState === 4 && request.status === 200 ) {
var JSONObject = JSON.parse(request.responseText);
createTable(JSONObject);
} else if(request.status == 400) { console.log("Error", request.status);}
};
request.open("GET", json, true);
request.send();
}
通过 XMLHttpRequest() 请求请求 JSON 文件。
JSON 对象的简短示例:
{
"meta": {
"version": 1,
"type": "test"
},
"reports": [
{
"report-metadata": {
"timestamp": 1528235303.721987,
"from-ip": "0.0.0.0"
},
//and so on...
目前仅尝试展示metaDataTable 表中的部分:
function createTable(jsonData){
$(document).ready(function(){
$('#table').dataTable({
data: jsonData,
columns: [
{ data: 'meta.type' },
{ data: 'meta.version' }
]
});
});
}
索引.html part:
<table id="table" class="display" style="width:100%"></table>
运行时只得到表中没有可用数据,我显然遗漏了一些东西。