I want show array of objects inside an array of objects.
I want show array of objects inside an array of objects.
GhaLzai
Posts: 8Questions: 2Answers: 0
This Is i am trying append to my datatable column.
columns: [
{ data: [data:'children.name']},
],
My json response looks like this.
"doc": {
"createdDate": "2022-05-28T15:38:27.043Z",
"enabled": true,
"_id": "6292a5756774d60c13b473a0",
"CNIC": "936123f9bc287e62af987261d2586c3f",
"employeeID": "UP81561",
"fName": "Test Employee",
"company": "6249ff221399dc7a14173dd1",
"fatherName": "Rob",
"motherName": "Sasha",
"spouse": "Mary",
"children": [
{
"_id": "6292a5756774d60c13b473a1",
"name": "Pete",
"age": 5
},
{
"_id": "6292a5756774d60c13b473a2",
"name": "Pete",
"age": 5
},
{
"_id": "6292a5756774d60c13b473a3",
"name": "Pete",
"age": 5
},
{
"_id": "6292a5756774d60c13b473a4",
"name": "Pete",
"age": 5
}
],
"__v": 0
}
its not appending data into this.
Mai main array is "doc" and inside that main array I have 'children"array of objects.
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Answers
@kevin how can i loop through this data?
Sounds like you need to use
ajax.dataSrc
to point Datatables to your row data. Something like this:Then just set
columns.data
like this:Kevin
@kevin I have updated m code. kindly check the Children array is already in "doc" array So I have to access childrent object which further has array of objects.
Just update the
ajax.dataSrc
to match your JSON response structure. Maybe something like this:Kevin
no it effects my other doc attributes.
my full code looks like this
` <script type="text/javascript">
where children is also a array of objects.
Sorry, I thought you were just trying to show the
children
. You can usecolumns.render
to loop through the children array to show all the names in the same cell.Kevin
can you provide the code example of my case?
First
doc
needs to be an array of objects. See the ajax docs for more details. I copied your example data into this test case:http://live.datatables.net/kucejoxe/1/edit
I put the row data in an array and used
columns,render
to render all the names into the cell.Kevin