I get Json data from server in order to display information by using DataTables.
In this json, there are rows, where in a column there may exist more than one value, so it's a multidimensional array as follows (I show just an excerpt of the array):
{
"info_table": [
{
"date": "2015-01-06",
"subject": "Some subject or title",
"type": "article",
"url": null,
"pdf": null,
"notes": null,
"created_at": "2015-06-26 13:38:53",
"updated_at": "2015-06-26 13:38:53",
"institute": "Some Institute name",
"researchers": [
{
"name": "CARL SMITH"
}
],
"assistants": [
{
"name": "YULIA SMIRNOVA"
}
]
}
]
}
The DataTable works fine so far:
$('#notes_table table').append('<thead><tr><th>Researchers</th><th>Date</th></tr></thead><tbody></tbody>');
$.each(response.info_table,function(i,item){
$('#notes_table').find('tbody').append('<tr><td>'+item.researchers+'</td><td>'+item.date+'</td></tr>');
});
The date
column values are displayed fine, however, for the researchers
column only [object Object]
is displayed. If I try to use a nested $.each() as follows:
$('#notes_table table').append('<thead><tr><th>Researchers</th><th>Date</th></tr></thead><tbody></tbody>');
$.each(response.info_table,function(i,item){
$.each(item.researchers, function(j,item2){
$('#notes_table').find('tbody').append('<td>'+item2.name+'</td>');
});
$('#notes_table').find('tbody').append('<td>'+item.date+'</td>');
});
I don't get anything, I just see a DataTables message saying Sorry, no results found
.
What am I missing? Any ideas?
Solution
Thanks to BLSully:
The working code looks as follows:
var table = $('#table_id').DataTable({
columns: [{
data: 'researchers[, ].name',
title: 'Researchers'
}, {
data: 'date',
title: 'Date'
}]
});
table.rows.add(data).draw();
And that was it.
rows.add()
method as well as column definitions – BLSully 2 days ago