Creating the tree structure from database data and convert to JSON

926 views
Skip to first unread message

roopjay

unread,
Apr 30, 2012, 1:37:57 PM4/30/12
to Discuss jquery.dynatree.js
We have a database that has the child_id, parent_id and the other
details for each row of a tree in the database. I am looking for the
best method to read the data from the database recursively and create
the JSON not knowing how deep the child items run under each root
node. I want to build the tree upfront and not lazyload. Does InitAjax
take a parent id as a parameter and recursively build the tree or does
it need the entire tree in the JSon format built in the server code
and fed to it? I was looking for your example server code to get some
ideas but didn't find it anywhere (/getTopLevelNodesAsJson). Any
pointers, help is greatly appreciated.

mar10

unread,
Apr 30, 2012, 3:45:17 PM4/30/12
to dyna...@googlegroups.com
initAjax takes the same args as jQuery's $.ajax() so you can pass a 'url' and as many 'data' as you like.
The resulting JSON would then contain the entire (recursive) node structure, i.e. nodes with childLists that again contain nodes, ...

I would think that it is way more performant to build everything you need server side within one roundtrip.
But if you prefer, you could to split it into recursive $.ajax() calls to assemble the data structures client side (and then use addChild() to append it to the tree)

hth
Martin

tha...@gmail.com

unread,
May 23, 2012, 10:59:36 PM5/23/12
to dyna...@googlegroups.com
same here 
did you find an easy way to do that
or at least to convert XML into JSON
 

Fred Lunau

unread,
Oct 2, 2013, 7:21:33 AM10/2/13
to dyna...@googlegroups.com
Hi,

I know this post is over a year old, but for those looking to create a UL structure recursively (serverside) from a 2-column DB table, I use  Zebra_Mptt to generate the PHP array structure recursively.  It even has a "to_list" function that takes that resulting array structure and magically turns it into a UL structure, that you feed into the Fancytree creation.  By modifying this code a bit, you can also add in additional attributes (serverside) based on the extra column data in your DB.  If you have a very large number of records to process, it may take a few seconds to render, but works wonders.
Reply all
Reply to author
Forward
0 new messages