[Betternestedset-talk] Trouble with tree view

Claudio Poli masterkain at gmail.com
Mon May 28 08:54:59 EDT 2007


hi Mark,
thanks for sharing this useful piece of code.
does you or somebody have a hint for this problem?

- root1
-- subcategory
-- subcategory
- root2
-- subcategory
-- subcategory
--- subcategory

ie. what's the usual setup to have multiple roots and to display them  
in a view, maybe ordered in some ways?

currently I'm using
<% for root in @categories %>
	<ul>
	<% for node in root.full_set %>
		<li><%= '-' * node.level %> <%= link_to node.name, category_path 
(node) %></li>
	<% end %>
	</ul>
<% end %>

with

@categories = Category.roots

thanks.

Il giorno 27/mag/07, alle ore 20:05, Noten Mark ha scritto:

> Hi all,
>
> after some extensive searching I found an recursive algorithm to  
> dislay a tree
> structure on http://snippets.dzone.com/posts/show/1919. I modified  
> it only
> slightly and I thought I would share the code with you.
>
> In application_helper.rb:
>
>  def display_tree_recursive(tree, parent_id)
>     ret = "<ul>\n"
>     tree.each do |node|
>       if node.parent_id == parent_id
>         ret += "\t<li>"
>         ret += yield node
>         ret += display_tree_recursive(tree, node.id) { |n| yield  
> n } if
> node.all_children_count > 0
>         ret += "</li>\n"
>       end
>     end
>     ret += "</ul>\n"
>   end
>
> You can call it in the view show.rhtml with @tree = node.full_set and
> @parent_id = node.parent_id where node = BNSModel.find(params[:id])  
> set in
> the show action:
>
> <%= display_tree_recursive(@tree, @parent_id) {|node| node.name}  %>
>  (Actually I use another helper method to avoid the code block in  
> the view).
>
> With the "if node.all_children_count > 0" statement, no extra query  
> is needed
> to determine if the node has any children. ;-)
>
> With some CSS magic (wrap it with <div id="tree"> and defining  
> layouts for
> <ul> and <li>) you can get a really nice looking tree. Maybe  
> someone has an
> example.
>
> Kind regards,
>
> Mark
>
> On Friday 25 May 2007 10:12, Mark.noten wrote:
>> Some extra information:
>>
>> manager.all_children = child_agents
>>
>> And agents.shift should be child_agents.shift
>>
>> Kind regards,
>>
>> Mark
>> --------- Original Message --------
>> From: betternestedset-talk at rubyforge.org
>> To: betternestedset-talk at rubyforge.org <betternestedset- 
>> talk at rubyforge.org>
>> Subject: [Betternestedset-talk] Trouble with tree view
>> Date: 25/05/07 00:23
>>
>>> 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 =&gt; :product_id.
>>>
>>> I would like to generate the below HTML code that displays a  
>>> tree. The
>>
>> ASCII
>>
>>> connectors can easily be replaced by icons.
>>>
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;&lt;b&gt;node
>>> 1&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;|-&lt;/td&gt;&lt;td nowrap&gt;node
>>
>> 1.1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
>>
>>> &lt;/tr&gt;
>>> &lt;tr&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;|-&lt;/td&gt;&lt;td nowrap&gt;node
>>
>> 1.2&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
>>
>>> &lt;/tr&gt;
>>> &lt;tr&gt;&lt;td&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;|&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td
>>
>> nowrap&gt;|-&lt;/td&gt;&lt;td nowrap&gt;node
>>
>>> 1.2.1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
>>> &lt;/tr&gt;
>>> &lt;tr&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;|&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td
>>
>> nowrap&gt;-&lt;/td&gt;&lt;td nowrap&gt;node
>>
>>> 1.2.2&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
>>> &lt;/tr&gt;
>>> &lt;/table&gt;
>>> &lt;/td&gt;&lt;/tr&gt;
>>> &lt;tr&gt;
>>> &lt;table border=0 cellspacing=0 cellpadding=0&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;-&lt;/td&gt;&lt;td nowrap&gt;node
>>
>> 1.3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
>>
>>> &lt;/tr&gt;
>>> &lt;/table&gt;
>>> &lt;/td&gt;&lt;/tr&gt;
>>> &lt;tr&gt;&lt;td nowrap&gt;node 2&lt;/td&gt;&lt;/tr&gt;
>>> &lt;/table&gt;
>>>
>>> I have this method defined in the AgentsHelper module:
>>>
>>>   def show_agents_tree(current_agent, child_agents)
>>>     html = &quot;&lt;table class=&quot;agent-tree&quot;&gt;n&quot;
>>>     html += &quot;&lt;tr&gt;&lt;td
>>
>> nowrap&gt;#{agent_link(current_agent)}&lt;/td&gt;&lt;/tr&gt;n&quot;
>>
>>>     if !agents.empty?
>>>       html += &quot;&lt;tr&gt;&lt;td&gt;n&quot;
>>>       html += show_agents_tree(agents.shift, child_agents)
>>>       html += &quot;&lt;/td&gt;&lt;/tr&gt;n&quot;
>>>     end
>>>     html += &quot;&lt;/table&gt;n&quot;
>>>   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
>>> _______________________________________________
>>> Betternestedset-talk mailing list
>>> Betternestedset-talk at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/betternestedset-talk
>>
>> ________________________________________________
>> Message sent using UebiMiau 2.7.10
>>
>>
>> _______________________________________________
>> Betternestedset-talk mailing list
>> Betternestedset-talk at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/betternestedset-talk
>
> -- 
> Met vriendelijke groet,
>
> Mark Noten
> Software engineer
> ITFC gcv
> Email: mark.noten at itfc.be
> GSM: +32 (484) 698 333
> _______________________________________________
> Betternestedset-talk mailing list
> Betternestedset-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/betternestedset-talk



More information about the Betternestedset-talk mailing list