1

我需要一些帮助,因为过去几天我一直在研究 repo 的问题/PR,以确定如何在资源集合中按字母顺序排列资源的 belongs_to 关联。使用 Rails5.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`

现在,它看起来像这样。集合的图像

我试过使用 order 选项,但根据文档,不幸的是,它仅适用于下拉菜单,而不适用于表格。有没有办法让列根据其名称按字母顺序排列,或者我必须手动完成?

4

0 回答 0