Datatables ajax call REST api
Datatables ajax call REST api
Hello everyone!
For a school project i am creating an API and wanted to implement Datatables. I'm still quite new to programming so bear with me. I've scanned through a lot of examples on the site but can't quite get the answer i'm looking for.
What i'm trying to achieve is populating a Datatable with data from a simple API i created.
The html table:
"
_id | name | position |
---|
"
datatables.js:
$(document).ready(function() {
$('#campaignTable').DataTable( {
"ajax": {
"url": "/targets",
"dataSrc": "data"
},
"columns": [
{ "data": "_id" },
{ "data": "name" },
{ "data": "email" },
{ "data": "position" },
]
});
}) ;
**Output of api route 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
}
]
}
i am able to populate the table with a simple variable using the same structure, but when using an ajax call it gets a bit difficult.
Any help is appreciated, Thanks!