tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

I would like advice on perlish, and ideally Catalyst-y ways, to create a web interface to display interactive tree data structures, pulling from an underlying db. The db data underlying the tree would look like this, and the web page should have an interface similar to this. That was from an ms-access form discussed here. The form (some kind of active x thing I think) has a lot of bells and whistles: nodes open up when you click on the plus, you can specify how many levels the tree should expand to, etc.

I don't expect a web interface to have the kind of flash that the access form had, but how close can I get? Is there any hope of approaching this kind of prettiness/functionality with a pre-cooked catalyst helper? Or some other way to do this with catalyst/perl? Maybe even with this newfangled AJAX thing, or something?

If possible, I would like to be able to insert new nodes into the tree from the web interface. I do *not* need to be able to rearrange existing nodes, or delete nodes/trees. (If there's functionality to do this, great, just for my purposes I probably wouldn't be needing this.)

If there are no perlish modules/sample apps that already do this, I would also appear pointers to solutions that do this in other non-perl (but still webby) ways.

PS I have started a bookmark collection on implementing trees from an underlying db, and will update this with good tips I get from any ensuing perlmonks discussion.

UPDATE: updated question to be more catalyst centric

UPDATE 2: From the catalyst list, Carl Franks advised me: You might check out Sam Tregar's HTML::PopupTreeSelect and the recent HTML::PopupTreeSelect::Dynamic which supports AJAX.

http://search.cpan.org/~samtregar/HTML-PopupTreeSelect-1.5/
http://search.cpan.org/~samtregar/HTML-PopupTreeSelect-Dynamic-1.0/

As far as I remember, it has hardcoded HTML and restricts you to HTML::Template - but the code may be useful, as might the JS library he uses.

Carl

UPDATE 3: I've narrowed my googling down to "perl treeview," and that is getting better results. Perl Treeview Project using an Array of Hashes buffering a MySQL Database isn't catalyst, but it looks like a good place to start.

UPDATE 4: Found another example of trees in cgi at A Tree Grows in Perl

UPDATE 5: DHTML Tree: not perl, not catalyst, but real simple, and I like that. Maybe I could get this working from the db by modifying the javascript somehow. However, I haven't worked with dhtml, but I am dimly suspecting there is a dark side... browser compatatibility? something. more research needed...

  • Comment on Catalyst Interface to Display Tree Structure Pulled from DB.

Replies are listed 'Best First'.
Re: Web Interface to Tree Structure Pulled from DB.
by dorward (Curate) on Dec 12, 2005 at 10:02 UTC

    I'm not aware of any modules to do this (which is not to say they don't exist, just that I've never had the need to do something like this), but its a relatively simple problem.

    The first thing to do is to get the data structure in place. Something like this might do the job:

    { name => 'node_name' id => 'node_id' children => [ # Hash refs just like the example here # but nested ] }

    You could populate it by getting the data with a few SQL queries. First grab the nodes where there is no parent id, then, for each one grab the nodes where the id is the same as the node you are looking at. A recursive function is likely to be your friend here. (Solutions involving grabbing all the data at once and then finding children in Perl might be more efficient).

    Next you implement the view in HTML. Outputting a series of nested lists is probably a good bet. You can include an extra list item in each on with an "Add node here" link.

    You can them implement expanding/collapsing nodes with JavaScript, or use Ajax to avoid having to leave the page to fill in details of the new node and then repopulate the entire tree.

      Thanks for your wisdom. Actually, I was hoping to avoid the recursiveness of the query, and speed up retrieval, by using ikegami's idea of an "all ancestors" table at Re: Tree Structure and Db. But your advice on the nuts and bolts of the interface, and assurance that it is doable, are appreciated.
Re: Catalyst Interface to Tree Structure Pulled from DB.
by dragonchild (Archbishop) on Dec 12, 2005 at 14:13 UTC
    Tree::Persist is what you're looking for. That will give you a Tree datastructure. Beyond that, I don't know about the display aspect of things.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Catalyst Interface to Display Tree Structure Pulled from DB.
by trwww (Priest) on Dec 14, 2005 at 18:48 UTC

    Here is my attempt at a web based "explorer" like treeview:

    http://waveright.homeip.net/products/FavoritesAnywhere.html

    You would have to hack what you needed out of the javascript and its not pretty, but it has DHTML CRUD for the nodes.

    I generated the HTML using XML::Writer and then XSLT

    I know it dosen't help much in terms of code you can reuse, but it might give you some ideas.

    trwww