大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 在下拉列表中的树结构中显示类别/子类别,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
root_category first_sub_category sub_sub_category another_sub_sub_category second_sub_category another_root_category first_sub_category second_sub_category
这是我的控制器:
def new @category = Category.new end
这里的看法:
<%= f.label :parent_category %> <% categories = Category.all.map{|x| [x.name] + [x.id]} %> <%= f.SELEct(:parent_id,options_for_SELEct(categories),{},class: 'form-control') %>
请帮忙.
def subcat_prefix(depth) (" " * 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
并在我看来使用它们
<%= f.SELEct(:parent_id,options_for_SELEct(category_options_array),class: 'form-control') %>
以上是大佬教程为你收集整理的ruby-on-rails – 在下拉列表中的树结构中显示类别/子类别全部内容,希望文章能够帮你解决ruby-on-rails – 在下拉列表中的树结构中显示类别/子类别所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。