View Full Version : Modifying tree code for dynamic attachment

03-11-2004, 03:17 PM

I have the following program which outputs a dynamic tree to the screen. The program works and is commented. I am trying to modify it to suit my needs and I need a little help.

This is the sequence of events I wish to achieve:
1) The Server outputs a dynamic file containing only the root nodes.
2) The initial tree is drawn (only root nodes).
3) When a user clicks on a root node, another Server call is made, which outputs a file containing all the children of that root node.
4) The children are attached to the root node, and displayed on the screen.

All the Server-side stuff is already done. I just don't know how to update a dynamic tree with dynamic data.

If anyone could take a look at the code (fairly simple to understand), and offer any help or suggestions it would be greatly appreciated.


03-11-2004, 06:53 PM
So you've explained what the code currently does.

I'm not sure I understand what you want it to do now. I just skimmer through the code, but I didn't see any server-side stuff or any notion of a dynamic tree.

What I saw was a static tree where different parts of it are either hidden or shown.

So I'm not sure I understand what you're talking about here.

I did once implement a 'dynamic' tree once, where my tree structure was stored in a database. I used PHP and Javascript to display the tree and it was simple, yet complicated. If this is what you're trying to do then I'll have to wait until I get home to find that code. But you'd have to either be using MySQL and PHP and setup your database accordingly for my code to plug and play, or else you'll have to figure out my code and change it to suit your own needs.

Again let me know what you're trying to do exactly, and I promise to look for that code (not promise that I'll find it necessarily...) this evening if you need it.


03-11-2004, 10:57 PM
Thanks for your response.

Yes currently the code is written to be static. I would like to break it apart so I can make it dynamic.

I already set it up so that if I query my database, it outputs a js page similar to the tree_items.js file. I am using Java Servlet->DB->JSP to do this.

Now I just have to take that data and 'plug' it into the existing tree at the correct root node.

03-12-2004, 12:00 AM
When do you want this to happen, in other words, when do you over-write the tree_items.js file? Because it is at this time that you will need to update the masterTree[someID] to reflect a new tree. You just need to figure out when you're going to want to make this happen, and make it happen.

I think I would better understand what we're trying to accomplish if you would explain the overall problem.


03-15-2004, 02:30 PM
ok, here's the exact problem.

On initial loading, the application queries a database and gets all top-level folders (root nodes) that the user has permission to.

When a user clicks on a top-level folder (root node), the application queries the database and returns all folders that exist underneath that top-level folder (including any subfolders).

So initially, it's a display of only root nodes.
After a user clicks a root node, it will get the entire tree for that root and attach it to that root node.

I don't think I could overwrite the initial .js file or else the tree roots wouldnt exist. Maybe I'd have to dynamically update the tree through another .js page? I don't want to keep passing info about the root nodes (and any trees that have been opened) to the application via POST or GET, because This could be huge.

03-15-2004, 06:51 PM
There is code out there that does exactly what you're talking about. You're going to need to get object-oriented with PHP in order to accomplish a clean solution, and pay very little attention to the Javascript (till later...).

Try these links, but search for your own as well:

I've done something exactly what you're talking about, but I did it a looong time ago! I'll have to search for that code, but I don't think I've got it on my computer any more...

Hope that helps,

03-15-2004, 07:11 PM
Thanks for your response.

Unfortuantely I am not allowed to use PHP in this application (for numerous business reasons). I can only use javascript and java Servlets.

The Servlets give me the info I need, and the javascript needs to link the info together.

I already have the Servlet side complete, and have the static tree complete. My only task left is to connect them dynamically.

btw, your second link is the tree program that I have zipped and posted at the top of the thread :)

03-15-2004, 07:49 PM
How about JSP? From my understanding PHP and JSP are very similar, just different syntax...

Here's another link that may be of assistance.

Just so you know, I don't think it's a simple task to merely convert a static javascript tree code to make it dynamic.

As I mentioned in an earlier post in this thread, you can figure out when a your servlet will be giving your javascript a new tree to work with, and then simply give that swap the contents of the tree. In other words, just call makeTree() again. Ensure you pass the proper ID, and then using TREE_ITEMS (or whatever you want...), it will automatically use your new file, thereby using your new tree.

Now consider another implication of using a "flat file" for this. Let's say user 1 starts opening nodes and getting your servlet to change the tree_items file. Then a few minutes later, user 2 wants to look at the tree. Will user 2 see the same thing as user 1? Because they are after all using the same file tree items file... Unless you've taken care of this, and once a user has logged in (since the application takes user permissions into account..), a new file in a different directory is used for that user...

A perhaps better way would be to store the tree structure in the memory or in the actual javascript file that the user sees. And have that information be passes back and forth in an array through the GET method..

Hope that makes sense... let me know what you think. I understand that you've probably created your servlet to work with this javascript tree. But so you're aware, I don't think this is the way to go about solving this...

Good luck,

03-15-2004, 08:06 PM
I think you're getting confused on what I'm saying. I am using java Servlets behind the scenes to dynamically generate a .js file. So it doesn't really matter what the back end is. The output is always the same...a .js file. Therefore we can pretty much pretend the whole servlet thing doesn't even exist. All we see is a dynamic js file.

So we can think of it like this...

I have a .js file that contains all the kids of a specific root node.
Once a user clicks on the root node, I need a way to connect this dynamic .js file to the existing tree.