Reputation: 2718
I'm very new to this whole area of js and datatables.
How can this be represented in datatables
var data = {"jaxm":
{
"name": "Tiger Nixon",
"position": "System Architect"
},
"jaxb" :
{
"name": "Garrett Winters",
"position": "Accountant"
}
}
I tried
$(duTable).DataTable({
data: data,
});
not sure if this possible without arrays or not but even I couldnt print single row in datatables using
$(duTable).DataTable({
data: data.jaxm,
});
Upvotes: 2
Views: 2239
Reputation: 85528
You cannot (the short answer). The above is more like a hash table, or a kind of associate array: You have an object with multiple keys (not indexes) each of them holding an object. You need to sanitize such construct into an indexed array :
function sanitizeData() {
var d = [];
Object.keys(data).forEach(function(key) {
d.push(data[key]);
});
return d;
}
var table = $('#example').DataTable({
data: sanitizeData(),
// **and** instruct dataTables which object property belongs to which column
columns: [
{ data: 'name' },
{ data: 'position' }
]
})
See demo -> http://jsfiddle.net/0c52ra0c/
It is basically just an algorithmic version of
var table = $('#example').DataTable({
data: [data.jaxm, data.jaxb],
columns: [
{ data: 'name' },
{ data: 'position' }
]
})
which would do the same. So you could use data: [data.jaxm]
as a solution for the last attempt of "print single row". Either way, you need to pass an indexed array to dataTables.
Upvotes: 3