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 ofa> node. A node in this case is an Agent model extending ActiveRecord::Baseand> using acts_as_nested_set :scope => :product_id. > > I would like to generate the below HTML code that displays a tree. TheASCII> 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>node1.1</td></tr></table>> </tr> > <tr> > <table border=0 cellspacing=0 cellpadding=0> > <tr><td nowrap>|-</td><td nowrap>node1.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><tdnowrap>|-</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><tdnowrap>-</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>node1.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><tdnowrap>#{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 > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > >________________________________________________ Message sent using UebiMiau 2.7.10
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 => :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 > > _______________________________________________ > > 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
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 => :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 >>> _______________________________________________ >>> 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
Hi Claudio, if you look at the bottom of this link http://snippets.dzone.com/posts/show/1919 then you''ll find what you need. You can use the display_tree_recursive function that I posted in your view like this: <%= display_tree_recursive(@tree, nil) %> In the action of your controller just put something like: @tree = Category.find(:all, :order => ''product_id, lft'') If have multiple roots with :scope => :product_id so the above statement sorts each tree by product_id (1, 2, ...) and then each tree is sorted with the root first, then it''s children from left to right, ... (like normally when working with better nested set). I''m sure other people have a better solution because this one generaties quite some calls to the display_tree_recursive function. Claudio, you also better but your code in an helper method to keep a clean view. Kind regards, Mark On Monday 28 May 2007 14:54, Claudio Poli wrote:> 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 => :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 > >>> _______________________________________________ > >>> 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 > > _______________________________________________ > 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