# Parsing a nested set another way

**URL:** <https://www.ruby-forum.com/t/parsing-a-nested-set-another-way/166478>\
**Category:** Ruby\
**Created:** [April 25, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/parsing-a-nested-set-another-way/166478 "2009-04-25T22:41:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aneeley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@aneeley](https://www.ruby-forum.com/u/aneeley)\
**Post date:** [April 25, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/parsing-a-nested-set-another-way/166478/1 "2009-04-25T22:41:43Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![aneeley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@aneeley](https://www.ruby-forum.com/u/aneeley)\
**Post date:** [April 26, 2009, 1:29am UTC](https://www.ruby-forum.com/t/parsing-a-nested-set-another-way/166478/2 "2009-04-26T01:29:43Z")

</div>

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](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 | &nbsp; |
|
| |

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

---

<div class="post-metadata">

**Author:** ![aneeley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@aneeley](https://www.ruby-forum.com/u/aneeley)\
**Post date:** [April 27, 2009, 1:46am UTC](https://www.ruby-forum.com/t/parsing-a-nested-set-another-way/166478/3 "2009-04-27T01:46:31Z")

</div>

Ian,

Thank you so much!
