Nested JSON column defs

Nested JSON column defs

johnmayjohnmay 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!

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    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:

    "columns": [
               { "data": "materialId" },
               { "data": "materialQuant" },
               { "data": "materialDesc" },
               { "data": "materialsCost" },
               { "data": "materialStatus" },
               { "data": "materialNotes" },
    

    If you are using ajax then you would use ajax.dataSrc, like the first example in the docs. Or if you are using data then you would use something like data: data.variationMaterials.

    Kevin

This discussion has been closed.