Hi all,
I’m fetching an Array of JSON Objects with AJAX and present them with DataTables. This has worked nicely as long as these objects weren’t nested, namely like this:
[ {id:'0', name:'x', links:'url0'},
{id:'1', name:'y', links:'url1'},
...
]
The problem now is some ‘links’ can have several values, so I want to use for this a Select2 dropdown menu.
I tried several things to do this and the best result so far was to get this:
[object Object]
I have looked many examples but I couldn’t find one similar to my case. I don’t know where should I put the select tag as well as how the JS should look like. I have modified my JSON to the expected format of Select2 and I can see it in the Chrome Console.
My HTML:
<table id="myTable" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Links</th>
</tr>
</thead>
<tbody>
<tr>
<td><select class="select2"></select></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Links</th>
</tr>
</tfoot>
</table>
My JS:
$(document).ready(function () {
$.ajax({
url: "/json",
type: "GET",
dataType: "json",
})
.done(function (json) {
console.log(json);
$('#myTable').dataTable({
"data": json,
"columns": [
{"data": "id"},
{"data": "name"},
{"data": "links"}],
select: true,
}),
$(".select2").select2({
data: json[10].links
})
console.log(json[10].links);
});
})
So how can I make Select2 and DataTables work together?