Rails Administrate-按字母顺序对Collection中的BelongsTo列进行排序

时间:2018-07-09 03:30:46

标签: ruby-on-rails ruby-on-rails-5 rails-administrate administrate

我需要一些帮助,因为过去两天中,我一直在研究回购的问题/ PR,以弄清楚如何按字母顺序在资源集合中订购资源的Emirates_to关联。 使用Rails 5.0.7和管理0.10.0

在我的情况下,我有一个Dish模型和一个Restaurant模型。菜属于餐厅,餐厅有很多菜。 我注意到,当人们点击餐厅的<th>时,它将根据餐厅的id而不是餐厅的name来订购该列。我想更改它,以便它根据餐厅的名称和字母顺序对列进行排序。下面的相关代码。

dish_dashboard.rb

class DishDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
    diets: Field::HasMany,
    restaurant: Field::BelongsTo.with_options(
      searchable: true,
      searchable_field: 'name'
    ),
    id: Field::Number,
    name: Field::String,
    description: Field::Text,
    picture: Administrate::Field::Image,
    price: Field::PriceField,
    price_currency: Field::String,
    deleted_at: Field::DateTime,
    published_at: Field::DateTime,
    created_at: Field::DateTime,
    featured: Field::Boolean
  }.freeze

  COLLECTION_ATTRIBUTES = [
    :name,
    :restaurant,
    :diets,
    :picture,
    :created_at,
    :featured,
  ].freeze
  ...
end

菜肴'_collection.html.haml

%table.collection-data{"aria-labelledby" => "page-title"}
  %thead
    %tr
      - collection_presenter.attribute_types.each do |attr_name, attr_type|
        %th{class: "cell-label cell-label--#{attr_type.html_class} cell-label--#{collection_presenter.ordered_html_class(attr_name)}", scope: "col", role: "columnheader", :'aria-sort' => "#{sort_order(collection_presenter.ordered_html_class(attr_name))}"}
          = link_to(sanitized_order_params.merge(collection_presenter.order_params_for(attr_name))) do
            = attr_name.to_s.titleize
            - if collection_presenter.ordered_by?(attr_name)
              %span{class: "cell-label__sort-indicator cell-label__sort-indicator--#{collection_presenter.ordered_html_class(attr_name)}"}
                &#9660;
      %th{:class => "cell-label"} ACTIONS
  %tbody
    - resources.each do |resource|
      %tr.js-table-row{tabindex: 0, :'role' => 'link', data: {url: "#{polymorphic_path([namespace, resource])}"}}
        - collection_presenter.attributes_for(resource).each do |attribute|
          - next if attribute.html_class === 'boolean'
          %td{class: "cell-data cell-data--#{attribute.html_class}"}
            = link_to polymorphic_path([namespace, resource]), class: 'action-show' do
              = render_field attribute
        - if current_page?(admin_dishes_path)
          - if resource.featured?
            %td
              = button_tag '', class: 'btn toggle-actvation-btn unfeature-btn', type: 'button', data: { 'id': resource.id }
          - else
            %td
              = button_tag '', class: 'btn toggle-actvation-btn feature-btn', type: 'button', data: { 'id': resource.id }
          %td
            = button_to 'Delete', admin_dish_path(resource.id), method: 'delete', class: 'delete-btn'`enter code here`

现在,它看起来像这样。 collection's image

我尝试使用order选项,但是不幸的是,根据文档,它仅用于下拉菜单,而不用于表格。 有没有一种方法可以让该列根据其名称的字母顺序进行排序,否则我将不得不手动进行此操作?

0 个答案:

没有答案