I’ve been stumbling on the tut at oriontransfer
(http://wiki.oriontransfer.co.nz/main/show/SortableTree)
I’ve got it working in a one dimensional list and I’ve got the drag and
drop working on children, but as can’t get it to save the tree.
Here’s my view:
Menu Prototype 2
<%= flash[:notice] %>
<% @navigation.each do |link| %>
-
<%= link.name %>
<% link.children.each do |child| %>
-
<%= child.name %>
<% end -%>
<% end -%>
<%= sortable_element ‘menu’,
:url => { :action => “sort”, :id => @navigation },
:complete => visual_effect(:highlight, ‘menu’), :tree => true
%>
Here’s the action:
def change_hierarchy
change_links params[:menu]
end
def change_links (links_hash)
parent_id = links_hash[:id] || Link.root.id
links_hash.each do |key, value|
next if key == "id"
link = Link.find (value[:id])
link.display_order = key
link.parent_id = parent_id
link.save
change_links value
end
end
Does anyone have a clue what’s my mess?
Hola,
He echo una adaptacion de tu codigo, a mí me funciona:
Mi condicion de categoria principal: (category.parent_id == nil)
Mi partial(_categories.html.erb):
<% @categories.all(:order => "show_order asc").each do |category| %>
<% content_tag_for :li, category do %>
<%= image_tag "drag.png" %>
<%= link_to category.name, blog_category_path(@blog.permalink,
category.permalink)%> (<%= category.posts.size%>)
" style="list-style:none;">
<% category.children.all(:order => "show_order asc").each do
|sub_category| %>
<% content_tag_for :li, sub_category do %>
<%= image_tag "drag.png"%>
<%= link_to sub_category.name,
blog_category_path(@blog.permalink, sub_category.permalink)%>
(<%= sub_category.posts.size%>)
<% end %>
<% end %>
<% end %>
<% end %>
<%= sortable_element 'categories', :url =>
sort_blog_categories_path(@blog.permalink),:complete =>
visual_effect(:highlight, 'categories'), :handle => "handle", :tree =>
:true, :update => "nav_zone" %>
Mi controlador:
def sort
i_sort params[:categories]
@categories = @blog.categories.root
render :partial => "categories/categories.html.erb"
end
def i_sort(categories_hash )
parent_id = categories_hash[:id] || nil
puts categories_hash.to_a.to_s rescue puts categories_hash.to_s
categories_hash.each do |key, value|
if key != “id”
category = Category.find(value[:id].to_i)
category.show_order = key
category.parent_id = parent_id
category.save
i_sort(value)
end
end
end
Suerte.