[Betternestedset-talk] Trouble with tree view

Noten Mark mark.noten at itfc.be
Thu May 24 17:12:29 EDT 2007


Hi all,

I'm struggling a bit with displaying a subtree with all the child nodes of a 
node. A node in this case is an Agent model extending ActiveRecord::Base and 
using acts_as_nested_set :scope => :product_id.

I would like to generate the below HTML code that displays a tree. The ASCII 
connectors can easily be replaced by icons.

<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap><b>node 1</b></td></tr>
<tr><td>
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap>|-</td><td nowrap>node 1.1</td></tr></table>
</tr>
<tr>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap>|-</td><td nowrap>node 1.2</td></tr></table>
</tr>
<tr><td>
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap>|&nbsp;&nbsp;</td><td nowrap>|-</td><td nowrap>node 
1.2.1</td></tr></table>
</tr>
<tr>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap>|&nbsp;&nbsp;</td><td nowrap>\-</td><td nowrap>node 
1.2.2</td></tr></table>
</tr>
</table>
</td></tr>
<tr>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td nowrap>\-</td><td nowrap>node 1.3</td></tr></table>
</tr>
</table>
</td></tr>
<tr><td nowrap>node 2</td></tr>
</table>

I have this method defined in the AgentsHelper module:

  def show_agents_tree(current_agent, child_agents)
    html = "<table class=\"agent-tree\">\n"
    html += "<tr><td nowrap>#{agent_link(current_agent)}</td></tr>\n"
    if !agents.empty?
      html += "<tr><td>\n"    
      html += show_agents_tree(agents.shift, child_agents)
      html += "</td></tr>\n"      
    end
    html += "</table>\n"
  end

It generates HTML code that just displays each agent as a child of the 
previous agent. Can anyone help me to get the right level (O, 1, 2, ...) 
dependent indentation working? Any help is appreciated.

Thanks,

Mark


More information about the Betternestedset-talk mailing list