对于一个学校项目,我正在创建一个 API,并希望实现 Jquery Datatables。我对 JavaScript 还是很陌生,所以请耐心等待。我浏览了 Datatables.net 网站上的许多示例,但无法完全得到我正在寻找的答案。我想要实现的是用我创建的简单 API 中的数据填充 Jquery 数据表。
html表:
<table id="campaignTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>_id</th>
<th>name</th>
<th>email</th>
<th>position</th>
</tr>
</thead>
<tfoot>
<th>_id</th>
<th>name</th>
<th>email</th>
<th>position</th>
</tfoot>
</table>
数据表.js:
$('#campaignTable').DataTable( {
"ajax": {
"url": "/targets",
"dataSrc": "data",
},
"aoColumns": [
{"columns": "_id" },
{"columns": "name" },
{"columns": "email" },
{"columns": "position" },
]
});
});
api 路由输出http://localhost:3030/targets : :
{
"total": 4,
"limit": 100,
"skip": 0,
"data": [
{
"_id": "577f6e04077321f331d8fbba",
"name": "test",
"email": "test@email.nl",
"position": "CEO",
"__v": 0
},
{
"_id": "577f6e08077321f331d8fbbb",
"name": "test1",
"email": "test@email.nl",
"position": "CEO",
"__v": 0
},
{
"_id": "577f6e0b077321f331d8fbbc",
"name": "test2",
"email": "test@email.nl",
"position": "CEO",
"__v": 0
},
{
"_id": "577f6e0d077321f331d8fbbd",
"name": "test3",
"email": "test@email.nl",
"position": "CEO",
"__v": 0
}
]
}
我能够使用相同的结构用一个简单的变量填充表,但是当使用 ajax/rest 调用时,它变得有点困难。其他一些示例表明它可能与 api 的 JSON 输出有关。但我不知道如何改变它。
任何帮助表示赞赏,谢谢!