使用 Awesome_nested_set 按字母顺序排序类别选择菜单
使用Rails 3 中的awesome_nested_set,我创建了一个分层类别系统。为了在视图中显示类别选择器,我使用了以下代码:
<%= form.select :parent_id, options_for_select(nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" }.unshift(["No Parent", nil]), @category.parent_id) %>
我正在尝试按字母顺序对类别进行排序,逐级为基础。如果我将nested_set_options(Category,@category)更改为nested_set_options(Category.order(“name”),@category)这将按名称重新排序整个类别列表;我想要做的是按名称字母顺序对每个节点的子节点重新排序。
例如,我希望结果选择菜单的顺序如下:
Animal
- Bird
-- Chicken
-- Hawk
- Fish
-- Cod
-- Goldfish
-- Trout
- Mammal
-- Cat
-- Primate
--- Chimpanzee
--- Human
-- Zebra
Plant
- Tree
Using awesome_nested_set with Rails 3, I've created a hierarchical categories system. To display the category selector in the view, I've used the following code:
<%= form.select :parent_id, options_for_select(nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" }.unshift(["No Parent", nil]), @category.parent_id) %>
I'm attempting to order the categories in alphabetical order, on a level by level basis. If I change the nested_set_options(Category, @category)
to nested_set_options(Category.order("name"), @category)
this will reorder the whole categories list by name; what I want to do is reorder the children of each node alphabetically by name.
For example, I want to resulting select menu to be ordered like this:
Animal
- Bird
-- Chicken
-- Hawk
- Fish
-- Cod
-- Goldfish
-- Trout
- Mammal
-- Cat
-- Primate
--- Chimpanzee
--- Human
-- Zebra
Plant
- Tree
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我不熟悉 Awesome_nested_set,但您可以在 Rails 3 中调用 order 两次。
这应该按每个级别对类别进行排序,然后按每个级别中的名称进行排序。另外,您可以将其放在模型内的默认范围内。
订单非常适合默认范围,因为它们不会影响任何默认值。
Although I am unfamiliar with awesome_nested_set, you can call order twice in Rails 3.
This should order Category by each level and then by name within each level. Also, you can throw this on the default scope within the model.
Orders are great for default scope because they don't effect any default values.
您可以按照此 stackoverflow 帖子中的建议使用
@item.children. except(:order).order("your_sort_column")
:很棒的嵌套设置顺序You can use
@item.children.except(:order).order("your_sort_column")
as suggested in this stackoverflow post: awesome nested set order by我有同样的问题。我按照此处所述解决了这个问题。基本上将以下方法添加到模型中,acts_as_nested_set 允许您通过调用 @category.sorted_heir_list 来返回视图中选择标记的对象的平面数组:
我比其他答案更喜欢这种方法我在评论中链接了 Morgan Christiansson 的答案,因为它只产生一次 DB 命中(相对于其他方法的 N 次命中)。
I had the same issue. I solved it as described here. Basically adding the below method to the model that acts_as_nested_set allows you to return a flat array of objects for the select tag in the view by calling
@category.sorted_heir_list
:I like this method better than the other answer that I linked in my comment on Morgan Christiansson's answer because it only incurs a single DB hit (vs. N hits with the other approach).