Show categories/sub-categories in tree hierarchy i

2019-02-02 18:43发布

I have a category table with fields id,name and parent_id. The root categories have parent_id 0. Now i want to show the list of categories in a drop down and a structure like this:

root_category
    first_sub_category
        sub_sub_category
        another_sub_sub_category
    second_sub_category
another_root_category
    first_sub_category
    second_sub_category

Here's my Controller:

def new
  @category = Category.new
end   

And here's the view:

    <%= f.label :parent_category %>
    <% categories = Category.all.map{|x| [x.name] + [x.id]} %>
    <%= f.select(:parent_id, options_for_select(categories), {}, class: 'form-control') %>

Please Help.

2条回答
女痞
2楼-- · 2019-02-02 18:52

Assuming you can get the children of a given category similar to:

has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'

Create a method for categories to get all children and indent each by the level:

def all_children2(level=0)
    children_array = []
    level +=1
    #must use "all" otherwise ActiveRecord returns a relationship, not the array itself
    self.children.all.each do |child|
      children_array << "&nbsp;" * level + category.name
      children_array << child.all_children2(level)
    end
    #must flatten otherwise we get an array of arrays. Note last action is returned by default
    children_array = children_array.flatten
end

Then in your view:

<select>
    <option></option>
    <% root_categories.each do |category| %>
      <option><%=category.name%></option>
      <% category.all_children2.each do |child| %>
        <option><%=child.html_safe%></option>
      <% end %>
    <% end %>
</select>

I haven't 100% tested this but the bits I did suggest it should work...

查看更多
叼着烟拽天下
3楼-- · 2019-02-02 18:56

Solved the problem by adding these functions in application_helper.rb

def subcat_prefix(depth)
  ("&nbsp;" * 4 * depth).html_safe
 end

 def category_options_array(current_id = 0,categories=[], parent_id=0, depth=0)
  Category.where('parent_id = ? AND id != ?', parent_id, current_id ).order(:id).each do |category|
      categories << [subcat_prefix(depth) + category.name, category.id]
      category_options_array(current_id,categories, category.id, depth+1)
  end

  categories
end

and using them in my view like this

<%= f.select(:parent_id, options_for_select(category_options_array), {}, class: 'form-control') %>
查看更多
登录 后发表回答