Sorting parent menu items in activeadmin

萝らか妹 提交于 2019-12-11 10:26:30

问题


I have a menu, with multiple :parent items. With :priority I can sort the items within a drop down menu.

How can I sort my main menu parent items? I'm using the latest version of activeadmin


回答1:


I helped myself with a hack, I found:

If you drop the following into the setup block of config/initializers/active_admin.rb.

It uses the priority of the first displayable child menu item to indicate the sort priority for the parent item.

  class ::ActiveAdmin::Views::TabbedNavigation
    def priority_for(item)
      child_item = item.children.detect { |child| display_item?(child) }
      child_item ? child_item.priority : item.priority
    end
    private :priority_for

    # Returns an Array of items to display
    def displayable_items(items)
      items.select do |item|
        display_item? item
      end.sort { |i1, i2| priority_for(i1) <=> priority_for(i2) }
    end
  end

This works fine for me.




回答2:


you can achieve the same within the default active admin initializer, no hacking required:

config.namespace :admin do |admin|
  admin.build_menu do |menu|
    menu.add label: "My first item", priority: 1
    menu.add label: "My second item", priority: 2
    menu.add label: "My third item", priority: 3
  end
end

# config/initializers/active_admin.rb



回答3:


on ActiveAdmin v=0.4.4 this little modified Version worked for me as an initializer:

class ::ActiveAdmin::Views::TabbedNavigation
  def priority_for(item)
    item.children.map(&:priority).min || item.priority
  end

  private :priority_for

  def menu_items
    menu.items.sort { |i1, i2| priority_for(i1) <=> priority_for(i2) }
  end
end


来源:https://stackoverflow.com/questions/13910416/sorting-parent-menu-items-in-activeadmin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!