Parent / child using JSON
Parent / child using JSON
lenamtl
Posts: 265Questions: 65Answers: 1
Hi,
I'm looking for an example of Parent / child I'm using PHP & JSON (no DB, no editor).
I would like to reproduce this kind of tables https://datatables.net/blog/2016-03-25
For example: the user select a company in parent then it display all locations in child.
I would like to use
var dataSet = [ ]
or this is not possible
{
"data": [
{
},
]
}
Thanks
This question has an accepted answers - jump to answer
Answers
To make sure I understand correctly. You want to store the child table data in the variable
dataSet
. When the parent row is clicked get the pertinent data fromdataSet
to display in the child table. If this is correct then yes you can do that.You will need a unique piece of data in the parent that can be used to find the child data in
dataSet
. IfdataSet
is an array then you will need to loop through all the elements to find the child data. Not efficient but could work with small amount of data.I would look at making it an object the the keys being the part to match from the parent and the value is an array of all the rows to display.
Kevin
Hi,
I can use any json / text forma.
The data are in excel file (around 400 lines) so I can transform to other format.
I do not use any DB for this project so any example that can work without a DB would be ok for the needs.
If possible without Ajax.
Thanks
You could absolutely do this without a database (and without Editor if you don't want editing). Basically follow along with the blog post you linked to (missing out the Editor parts), but instead of fetching the data from an Ajax source, you would need to query wherever you are storing your data and feed it into the child DataTable that way (e.g.
data
rather thanajax
).Allan
Hi,
Can you provide a basic example Parent / child that use data or dataset.
I would really appreciate.
Here is a simple example: http://live.datatables.net/zamoxabo/1/edit .
Allan
Hi @allan
I have many columns and rows, so I'm wondering which method would you use to convert excel data to this data format with parent / child?
Thanks
It isn't something I've done before, so I'm afraid I can't really advise on that. It will depend on the amount of data and the format it is in, but perhaps you could parse the data out of the XSLX file (SheetJS for example can do that) and then convert it to JSON.
Allan