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
"defaultContent": "<i>Not set</i>"to your column objects - if that doesn't work came back and we'll look further but I should imagine that is it: datatables.net/reference/option/columns.defaultContent