排序表关联的列滑轨

时间:2018-12-27 19:29:01

标签: ruby-on-rails sorting

请有人帮助我。我有一个与其他两个模型(客户端和工作站)关联的模型位置。

位置索引

<div class="table-wrap">
<table class="table table-striped ">
  <thead>
    <tr>
      <th><span class="relative"><%=  sortable "name", Location.human_attribute_name(:name) %></span></th>
      <th><span class="relative"><%=  sortable "name", Location.human_attribute_name(:client) %></span></th>
      <th><span class="relative"><%=  Location.human_attribute_name(:points) %></span></th>
      <th class="text-right"><%= t 'views.actions' %></i></th>
    </tr>
  </thead>
  <tbody>
    <% @locations.each do |location| %>
      <tr>
        <td><%= location.name %></td>
        <td><%= location.client.name %></td>
        <td><%= location.points.count %></td>
        <td class="actions text-right">
          <%= link_to content_tag(:i, '', class: "mdi mdi-pencil"), edit_location_path(location), class: "btn btn-sq-sm trigger-tooltip", data: {toggle: "tooltip", placement: "top", "original-title" => t('views.edit')} %>
          <%= link_to content_tag(:i, '', class: "mdi mdi-delete"), location, method: :delete, class: "btn btn-sq-sm trigger-tooltip", data: {toggle: "tooltip", placement: "top", "original-title" => t('views.delete'), confirm: t('views.delete_warning', model: Location.model_name.human).capitalize } if policy(Location).destroy? %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

位置控制器

def index
 @locations = policy_scope(current_company.locations).order("#{sort_column} #{sort_direction}")
end
.
.
.
  private 

def sort_column
 Location.column_names.include?(params[:sort]) ? params[:sort] : "name"
end

def sort_direction
 %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end

位置助手

module LocationsHelper
 def sortable(column, title = nil)
  title ||= column.titleize
  active = column == sort_column
  ascending = active && sort_direction == 'asc'
  direction_html = ascending ? ' ▲ ' : ' ▼ ' if active
  css_class = active ? "current #{sort_direction}" : nil
  direction = ascending ? "desc" : "asc"
  link_to(title, { :sort => column, :direction => direction }, :class 
  => css_class) << raw(direction_html)
  end
 end

如果我这样离开,我在两个表上具有可排序的“名称”的索引将无效。

<th><span class="relative"><%=  sortable "name", Location.human_attribute_name(:name) %></span></th>
          <th><span class="relative"><%=  sortable "name", Location.human_attribute_name(:client) %></span></th>

如何获取其他表的列?

Index table columns

0 个答案:

没有答案