in reply to tracking where I am in a tree structure
I think what you are going to want to do is to make the $withclickiness variable no longer optional in descend_htmltree, since you are checking it in htmltree_to_html you really dont need to anyway as it will already be set or 0.
Also, I think you actually meant to be passing that along your recursive call to descend_htmltree anyway, otherwise it would only do it for the top node.
Then once this is all in place, add a third variable, which will be the node-id you are looking for. Each time you pass the $node_id to the next recursive call, it will get appended with the $node_counter, which is just the depth in the current child group. This should then produce the node_id you are looking for.
Here is some code, I could not test is as you supplied no test data to test against. But it should work, let me know if you have any problems with it.
One another note, I am not 100% sure your HTML, in particular the onClick part of the DIV tag will work properly. The browser may see the entire contents of the DIV (it and all its subtrees) as all part of the same HTML container.sub descend_htmltree { my $node = shift; my $withclickiness = shift; my $node_id = shift; my $node_counter = 0; foreach my $tmpnode (@{$node}) { $node_counter++ if(ref($tmpnode) eq 'HASH') { my $nodeid = "${node_id}.$node_counter"; # Magic code to generat +e node's position in tree $htmloutput .= "<div style='border: thin solid #bbbbbb' onDblCli +ck=\"alert('you clicked $nodeid')\">" if($withclickiness); $htmloutput .= "<$tmpnode->{tag}"; foreach(keys %{$tmpnode}) { $htmloutput .= " $_=\"$tmpnode->{$_}\"" if($_ ne 'tag' && $_ n +e 'content'); } $htmloutput .= ">"; descend_htmltree($tmpnode->{content}, $withclickiness, $current_ +node_id); $htmloutput .= "</$tmpnode->{tag}>"; $htmloutput .= "</div>" if($withclickiness); } else { $htmloutput .= "$tmpnode"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: tracking where I am in a tree structure
by agaffney (Beadle) on Jul 22, 2004 at 22:07 UTC | |
by stvn (Monsignor) on Jul 22, 2004 at 22:20 UTC | |
by agaffney (Beadle) on Jul 23, 2004 at 03:26 UTC | |
by stvn (Monsignor) on Jul 23, 2004 at 04:07 UTC | |
by FoxtrotUniform (Prior) on Jul 23, 2004 at 01:38 UTC | |
by stvn (Monsignor) on Jul 23, 2004 at 03:50 UTC |