Hi Ron,
Not sure if this helps but I set up a tree recently. I am relatively
new to rails and so am not sure how to turn it into a helper, but would
be happy to work with others to do so.
My controller just uses the usual list function, modified to get all the
roots of the tree:
def tree
@root_pages, @roots = paginate :<your model>, :per_page => 5,
:conditions => [ "parent_id IS NULL" ]
end
Whichever model the tree is being used with needs to be declared
"acts_as_tree" and have the necessary parent_id column in the db.
Then the view is created from a recursive template call:
tree.rhtml
<% @indent = 0 %>
<%= render :partial => "node", :locals => {:new_roots =>
@roots,
:indent=> 0} %>
<br>
<%= pagination_links(@root_pages) %>
using the partial:
_node.html
<% for node in new_roots %>
<div id="node<%= node.id %>" style="margin-left:<%=
indent*30 %>px"
class="greenbox">
<a class="expander" href="#"
onclick="Toggle.display(''node<%= node.id
%>body'');
return false;">»</a>
<b><%= node.title %></b><br>
<div id="node<%= node.id %>body"
style=''display:none;''><%= node.text
%><br>
</div>
</div>
<% if node.children.size > 0 %>
<% new_indent = indent + 1 %>
<% if new_indent < 3 %>
<%= render :partial => "node", :locals =>
{:new_roots =>
node.children, :indent=> new_indent} %>
<% end%>
<% end%>
<% end %>
So this assumes that your model has the columns, id, text and title in
the db, and this recursive template call will give you an indented tree
structure, with links to toggle expansion of the individual nodes to
show the title, or title & text - this latter being done by a little
javascript.
Not sure if this meets your needs, but I would be interested to work on
improving this. I plan to add AJAX adding/deleting of nodes in the
tree. I guess it wouldn''t be too difficult to add something that
collapsed the individual parts of the tree - at the moment, everything
is displayed.
Oh and here''s the style for the nodes in the tree:
.greenbox {
border-top: thin solid #99CC66;
border-right: thin solid #99CC66;
border-bottom: thin solid #99CC66;
border-left: thin solid #99CC66;
padding: 5px;
margin: 8px;
background-color: #E0EFD1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
}
Let me know if that helps.
CHEERS> SAM
Ron M wrote:
>
> Anyone have a good tree control helper for Rails?
> Googling for ''"tree control" ruby rails"
didn''t show me anything obvious.
>
>
> Ideally one with fancy features like dynamic loading and
> drag&drop like this:
> http://www.obout.com/t2/edraganddrop.aspx
> http://www.obout.com/t2/eMSDN_DL_deep.aspx
> but simpler ones like these would be fine
> http://www.treemenu.net/treemenu/3fr_largetree.html
> http://www.treemenu.net/treemenu/3fr_checkbox.html
>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>