Reputation: 329
The thing is that i have one Datatable working very well that adds all 87 Star Wars characters from swapi.co API, the info comes in a JSON structure like this:
{
"count": 87,
"next": "https://swapi.co/api/people/?page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.co/api/planets/1/",
"films": [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
"species": [
"https://swapi.co/api/species/1/"
],
"vehicles": [
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
"starships": [
"https://swapi.co/api/starships/12/",
"https://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.co/api/people/1/"
}
This works very well and i add it like this:
var table = $('#example').DataTable( {
"columns": [
{ "data": "name" },
{ "data": "height" },
{ "data": "hair_color" },
{ "data": "skin_color" },
{ "data": "gender" }
]
} );
$.ajax({
url: 'https://swapi.co/api/people/',
dataType: 'json',
success: function(json){
table.rows.add(json.results).draw();
}
});
This works great but now i want to select one row (select one character from the datatable) and extract its url from the JSON, so i can fill another datatable with different info from the character JSON like this:
$('#example tbody').on('click', 'tr', function () {
var data = table.row( this ).data();
alert( 'You clicked on '+data.url+'\'s row' );
$.ajax({
url: data.url,
dataType: 'json',
success: function(json){
tableDos.rows.add(json.name).draw();
}
});
} );
It all works very well except the line when it is going to add it to the datatable. This JSON comes like this:
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.co/api/planets/1/",
"films": [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
"species": [
"https://swapi.co/api/species/1/"
],
"vehicles": [
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
"starships": [
"https://swapi.co/api/starships/12/",
"https://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.co/api/people/1/"
}
And the datatable comes like this:
var tableDos = $('#exampleDos').DataTable( {
"columns": [
{ "data": "name" },
{ "data": "gender" }
]
} );
It doesnt work and Datatables show this error http://datatables.net/tn/4
Upvotes: 0
Views: 4172
Reputation: 5699
You had an issue with you ajax as you were just adding the name
here: tableDos.rows.add(json.name).draw();
.
I had a couple of minutes spare so I took a longer look via JSFiddle and this seems to work:
$(document).ready(function() {
var table = $('#example').DataTable({
"columns": [{
"title": "Name",
"data": "name"
},
{
"title": "Height",
"data": "height"
},
{
"title": "Hair Colour",
"data": "hair_color"
},
{
"title": "Skin Colour",
"data": "skin_color"
},
{
"title": "Gender",
"data": "gender"
}
]
});
var tableDos = $('#exampleDos').DataTable({
"columns": [{
"title": "Name",
"data": "name"
},
{
"title": "Gender",
"data": "gender"
}
]
});
$.ajax({
url: 'https://swapi.co/api/people/',
dataType: 'json',
success: function(json) {
table.rows.add(json.results).draw();
}
});
$('#example tbody').on('click', 'tr', function() {
var data = table.row(this).data();
console.log(data);
$.ajax({
url: data.url,
dataType: 'json',
success: function(json) {
tableDos.row.add(json).draw();
}
});
});
});
You were adding a single object so this would work: tableDos.rows.add([json]).draw();
or, the method I used: tableDos.row.add(json).draw();
.
Hope that helps.
Upvotes: 1
Reputation:
https://datatables.net/forums/discussion/48819/help-filling-datatable-with-json-from-api
Kevin’s example didn’t work? http://live.datatables.net/midaqate/1/edit
If Kevin couldn’t figure it out - not sure what else to tell you.
For my server-side database, I used the regular sqli insert method. Found it more efficient.
Upvotes: 1