Forum: Ruby Parsing a nested set another way

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Adam N. (Guest)
on 2009-04-26 00:41
Attachment: Photo_49.jpg (0 Bytes)
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: . 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.
Ian H. (Guest)
on 2009-04-26 03:29
(Received via mailing list)
Adam Neeley wrote:
> (I am using Ramaze & DataMapper with nested_set plugin.)
> Any advice is appreciated. Thanks in advance.
> Attachments:
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 "<table  params>", and sets a global flag to say
that "<tr>" 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 "<tr>".
    generates <td rowspan="x"> where x it the number of leaves
calculated above.
    generates its contents, and closes itself with </td>
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   <td class="hash">&nbsp;</td>
    then it must close the row with </tr>
    and sets the flag to say a "<tr>" is needed
Then it exits

When the last child exits, generates the </table>

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

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.


Adam N. (Guest)
on 2009-04-27 03:46

Thank you so much!
This topic is locked and can not be replied to.