how to get data from json with tree format (2)
how to get data from json with tree format (2)
ycusoy
Posts: 27Questions: 5Answers: 0
how get and read to datatable if json format like this:
{
"china": {
"prov": "meizu",
"port": 4533,
"name": "hongkong"
},
"USA": {
"prov": "boston",
"port": 78574,
"name": "New York"
},
"Jakarta": {
"prov": "Java",
"port": 3457,
"name": "Irian"
},
"Israel": {
"prov": "tel Aviv",
"port": 008886,
"name": "Musrq"
},
.......
}
asume like
|country | prov | port | name
|china | meizu | 4533 | hongkong
|USA | boston | 78574 | New York
|Jakarta | Java | 3457 | Irian
|Israel | tel Aviv | 008886 | Musrq
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
I recently answered a similar question in this thread:
https://datatables.net/forums/discussion/comment/152746#Comment_152746
Does this help?
Kevin
I saw that and learned about it. it looks like there are different structures of data variables. I see there is regularity of the lines in each data head, so that it is able to be defined to the head of the title. I took part of the example:
whereas for the questions I submitted above, each of the heads does not have a regular definition. so I have difficulty entering it in the data table. data is only read from regular lines, for example the second row. while the head cannot be inserted.
Sorry, I meant to link to this thread. I thin the example I provide you in this thread should work. Did you try it?
https://datatables.net/forums/discussion/56528/how-to-get-data-from-json-with-tree-format#latest
Kevin
there I did not find an answer, it was more directed at repeating the title in the headline. what I mean above is different from before ...
My last post restructured your data to one that Datatables supports:
http://live.datatables.net/kumixemu/1/edit
I believe the above is the same structure and my example should work. Basically the problem is solved by looping through the JSON response to restructure it for Datatables. Or you can do this server side.
If you need further help please post an example or update mine with your data and your code to restructure the data. This will allow us a better opportunity to help you.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thank you for the answer, then I tried to change a little of the example you gave. I refer directly to using ajax in the command. the results are like this and do not show changes:
Its confusing to keep referring to your other thread but in that thread we were discussing using the
ajax.dataSrc
option. You posted a code snippet and asked what to put in the ajax.dataSrc function. I provided an example of how this could be done and noted that you should return the generated data instead of json.data. You still need to put this code in theajax.dataSrc
function and return the generated data.I think this is more what you are looking for:
Kevin
it works. great job. thank you for a very detailed explanation. I don't know what to say but thank you.
hi kevin, I followed the example at this and I met new problems and had difficulty defining the json format like this:
and I write in javascript:
but I don't see any data processed in the table.
In that example the
0
and other numbers are referencing an array elements. Your data structure doesn't have arrays. You are still in the same situation as before. Take a look at theajax
tab in that example. You will see that each row has the same structure, ie, each row as anhr
object, etc.Datatables expects your data to look more like this:
You columns definition would look more like this:
When the data is loaded Datatables will iterate through each element in the
data
array. Datatables doesn't directly support your data structure.However, after thinking about it, you can use a function with
columns.data
. Using theObject.keys()
method you could make your data structure work. For the first column you will need to get the object key then for the other columns you will need to get the value of each desired key. For example:http://live.datatables.net/tusaroqa/1/edit
This way you won't need to preprocess the data like I showed before. Hope this helps.
Kevin
the more I dive in and learn the examples you give, the more I want to explore the js in the datatable. very interesting to add ideas and develop insights about JS. thanks again for the help. greetings from me.
a little extra if you switch to json from the url.
toast
thank for kevin
And how if json data like this
i try same ex on top but not change. Why?
in edit mode http://live.datatables.net/wujozice/1/edit
This is array based data not object based. You will need to access using techniques for an array. I think it would be a bit complicated depending on how you want the data displayed. If you want
Boston
to be one row andDC
to be another then the above technique won't work as that array would be assigned to one row.It would be better to return the data in a structure that Datatables supports.
https://datatables.net/manual/data/#Data-source-types
Kevin
i try leave blank dataSrc and column data: data[0] cant do something. I am confused by the separation of the sign
[
or{
in data. because in fact every change in the sign also changes the order.finally I gave up. after several times trying and learning from several forums but found no answer there. the last time I was forced to exit the column (I don't think the data will be processed in dataTable.) and start from the ajax line. I know it's not a solution. until now I was forced to manually write the data and only use the css dataTable without js.