I'm trying to create a data set for datatables by accessing through a for loop and I want to reach nested arrays. Here is my loop:
function format(d) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
` <tr>
<td>{% trans 'contact-country' %}</td>
<td>${d.country}</td>
</tr>
<tr>
<td>Profession</td>
<td>${d.job}</td>
</tr>
<tr>
<td>{% trans 'contact-company' %}</td>
<td>${d.job_title}</td>
</tr>
<tr>
<td>Email</td>
<td>${d.email}</td>
</tr>
<tr>
<td>Expertises</td>
<td>${d.expertise}</td>
</tr>
<tr>
<td>Linkedin</td>
<td>${d.linkedin}</td>
</tr>
<tr>
<td>Description</td>
<td>${d.description}</td>
</tr>
<tr>
<td>Description</td>
<td>${d.id}</td>
</tr>
<tr>
<td>Description</td>
<td>${d.languages}</td>
</tr>
</tr>`
+
'</table>';
}
var tableData = []
for (let i = 0; i < list.length; i++){
tableData.push(
{
"first_name": list[i].user.first_name,
"last_name": list[i].user.last_name,
"circle": "circle",
"meeting": "meeting",
"favorites": [list[i].favorites, list[i].id],
"notes":list[i].id,
"country": "country",
"expertise": list[i].expertise, <=== Need to get each array in this data
"job": list[i].job,
"job_title": list[i].job_title,
"language":"language",
"email": list[i].user.email,
"linkedin": list[i].linkedin,
"description": list[i].about,
"id": list[i].id
}
);
}
Here is the result of the tableData array that I get
I need to be able to loop again in the expertise to get the original_name for each. A for loop inside the tableData.push() does not work.
And here is my DataTable :
$(document).ready(function () {
var table = $('#example').DataTable({
createdRow: function (row, data, dataIndex) {
$(row).attr('data-id', 'id');
},
"deferRender": true,
'columnDefs': dataLabel,
"paging": false,
"info": false,
data: tableData,
"aoColumns": [
{ "className": 'details-control',"orderable": false,"data": null,"defaultContent": '' },
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "circle" },
{ "data": "meeting" },
{ "data": "country", "visible": false },
{ "data": "expertise", "visible": false },
{ "data": "job", "visible": false },
{ "data": "job_title", "visible": false },
{ "data": "language", "visible": false },
{ "data": "linkedin", "visible": false },
{ "data": "description", "visible": false }, <===== Here is where I need the result
{ "data": "id", "visible": false },
],
"order": [[1, 'asc']]
});
Any solution to display those data through the for loop of via DataTable is welcome.