Nested JSON column defs
Nested JSON column defs
johnmay
Posts: 10Questions: 3Answers: 0
Hi
I have a datatable with the following columns definition...
"columns": [
{ "data": "variationMaterials[].materialId" },
{ "data": "variationMaterials[].materialQuant" },
{ "data": "variationMaterials[].materialDesc" },
{ "data": "variationMaterials[].materialsCost" },
{ "data": "variationMaterials[].materialStatus" },
{ "data": "variationMaterials[].materialNotes" },
Referencing documents in a MongoDB collections with the following structure...
{
"_id": {
"$oid": String
},
"variationID": String,
"variationTitle": String,
"variationDesc": String,
"custID": String,
"projID": String,
"variationStatus": String,
"variationCategory":String,
"variationCost": String,
"requireMaterial": String,
"variationRequestor": String,
"variationCreationDate": String,
"variationImages": [],
"variationMaterials": [
{
"materialId": 00000001,
"materialDesc": String,
"materialQuant": String,
"materialStatus": String,
"materialNotes": String,
"materialsCost": String
},
{
"materialId": 00000002,
"materialDesc": String,
"materialQuant": String,
"materialStatus": String,
"materialNotes": String,
"materialsCost": String
}
],
"__v": 0
}
So based on this document, I would expect the objects in variationMaterials to be rendered on two separate rows. Instead the values of each object in the array are rendered on the same row with multiple values per column.
Can anyone tell me where I'm going wrong?
Thanks!
This discussion has been closed.
Answers
Instead of configuring Datatables for nested objects you will need to direct Datatables to start processing the row data from the
variationMaterials
object. Your columns would like like this:If you are using
ajax
then you would useajax.dataSrc
, like the first example in the docs. Or if you are usingdata
then you would use something likedata: data.variationMaterials
.Kevin