Parsing a nested set another way


#1

Hello, new user here.

I am trying to render a nested set a certain way. I have code that
renders it as a list. But I want to render it another way that I know is
possible, but just can’t figure out how to do it. (Look at the
attachment for a sketch)

Here is my current code: http://pastie.org/458219 . This works, but is
not what I want. I think it might be close, though.

(I am using Ramaze & DataMapper with nested_set plugin.)

Any advice is appreciated. Thanks in advance.


#2

Adam Neeley wrote:

(I am using Ramaze & DataMapper with nested_set plugin.)

Any advice is appreciated. Thanks in advance.

Attachments:
http://www.ruby-forum.com/attachment/3622/Photo_49.jpg

Hi Adam,

Your source appears to be eruby (which I don’t know), so I will limit
myself to generalities, and pseudocode.

You wish to render the data, which is a tree, as a table, you need to
know two things before you can start.

a) The number of columns in the table (equals the depth of the tree)

b) The total number of leaves under each node. (The rowspan or depth of
that cell).

You will have to compute these by walking the tree. Visit the child
nodes before the parent node, telling each child node its depth. It is
to increase a global Depth variable if it finds itself deeper than the
global depth variable. Thus the global depth variable ends up containing
the number of columns the table must have.

Each child should return
1 if it has no children
The sum of the numbers returned by its children if it has children.
so that its parent can accumulate the rowspan of the cell it has to be
drawn in. A cell with two children will have to span two rows, one for
each child. Its parent will also have to span those two rows.

The table generation code then processes the nodes in “pre-order”,
generating the content of this node before all of its children.

A table starts with “

”, and sets a global flag to say
that “” is required (or “between rows”, before calling the top level
nodes one after the other (passing in column No 1).
Then each node genereates for itself
if the flag is set, it clears it, and generates “”.
generates
If it has children
it calls each of its children in turn (passing in the column number
it received, plus 1)
otherwise (it has no children),
generate the hashed cells to its right…
for i in range colNo + 1 to TableWidth
generate
then it must close the row with
and sets the flag to say a “” is needed
Then it exits

When the last child exits, generates the

where x it the number of leaves
calculated above.
generates its contents, and closes itself with
 

Set up the css to shade the hashed cells appropriately, and you are
done.

You can mess with column widths and depths, but I would do as little of
this as possible. The default will probably be just fine, and be driven
by the size of the content.

Regards

Ian


#3

Ian,

Thank you so much!