【问题标题】:How to display arrows based on sorting asc/desc (RoR 4)如何根据 asc/desc 排序显示箭头(RoR 4)
【发布时间】:2013-10-25 18:53:39
【问题描述】:

正如标题所说,我正在尝试显示向上/向下箭头。目前基于 Ruby 2.0 / Rails 4.0

我已经关注了有关对表格列进行排序的 railscasts (http://railscasts.com/episodes/228-sortable-table-columns),但他当然使用表格,因此他的箭头可以很好地显示。

我想使用 divs/spans/其他任何东西。 (这不是很多数据,也不是需要在表中的那种数据。)

话虽如此,我为我的箭头创建了一个应用程序帮助方法,它们可以工作,但是如果我单击一个作为 asc,两个箭头都指向上方。如果我为 desc 单击一个,则两个都指向下方。显然,因为params[:direction] 设置为 asc 或 desc,所以两个箭头都设置了。我如何将它们分开?有点伪代码:

if title && asc
  sort by title and asc && show up arrow (for title only)
if title && desc
  sort by title and desc && show down arrow (for title only)
if date posted && asc
  sort by created_by and asc && show up arrow (for date posted only)
etc.

我真的不想有一个巨大的 if/then 条件语句,但想要更简单的东西。

(如果迫在眉睫,我将只使用一个表格作为排序依据:部分,但这看起来真的非常愚蠢,这是最后的手段......)

这是我得到的代码:

show.html.erb

<h3>Images</h3>
<% if @user.images.any? %>
  <div class="sortable"><%= sortable "img_name", "Title" %><%= arrow %> | 
                        <%= sortable "created_at", "Date posted" %><%= arrow %>
  </div>  
  <%= render @images %>
  <%= will_paginate @images %>
<% end %>

application_helper.rb

def sortable(column, title = nil)
    title ||=  column.titleize
    css_class = column == sort_column ? "current #{sort_direction}" : nil
    direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
    link_to title, {sort: column, direction:  direction}, {class: css_class} 
end

def arrow
    if params[:direction] == 'asc'
        image_tag("arrow_up.png", alt: "up arrow", size: "15x15")
    elsif params[:direction] == 'desc'
        image_tag("arrow_down.png", alt: "down arrow", size: "15x15")
    else
        # either a blank image to show or just force no-display of image
    end
end

users_controller.rb

def show
  @user = User.find(params[:id])
  @images = @user.images.paginate(page: params[:page]).order(sort_column + " " + sort_direction)
end
private
  def sort_column
    Image.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
  end

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

【问题讨论】:

    标签: ruby-on-rails ruby sorting


    【解决方案1】:

    我不理解您不愿意在这里使用表格。即使它是一个小表,这个仍然是表格数据,有行、列和标题。为什么用 table-like div 结构替换语义表结构?

    话虽如此,我将扩展箭头助手以发送箭头所指的列:

    def arrow(column)
      # as you're not sorting on the column, you don't want to see the arrow at
      # all of the header is not for the sorted column
      return '' if params[:sort] != column
      # ...
    end
    

    导致模板看起来像:

    <%= sortable "img_name", "Title" %><%= arrow "img_name" %>
    

    虽然您会注意到这里有一些不必要的重复。由于这两个助手总是被一起调用,所以它们也可以加入:

    def sortable(column, title=nil)
      # ...
      capture do
        concat link_to(title, {...})
        concat " "
        concat arrow(column)
      end
    end
    

    生成更简单的模板:

    <%= sortable "img_name", "Title" %>
    

    【讨论】:

    • 嗯,这个我试过了,还是不行。两个箭头仍然同时指向上方或下方。我决定只为排序链接制作一个表格,并尝试让它以这种方式工作。 :\ 一旦我让它完全工作,我会发布我的解决方案。
    • 我想你一定错过了一些东西(或者我错过了一些东西)。请注意我的箭头助手中的第一行。应该只有一个箭头,当前正在排序的列的箭头。如果该列不是被排序的列,则返回一个空字符串而不是图像标签。
    【解决方案2】:

    如果其他人想使用 glyphicon 而不是像我这样的图像,此解决方案可能会很有用。在这个例子中我使用FontAwesome

    application_helper.rb

    def sortable(column, title = nil)
      title ||= column.titleize
      css_class = column == sort_column ? "current #{sort_direction}" : nil
      direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
    
      capture do
        link_to({sort: column, direction: direction}, {class: css_class}) do
          concat title
          concat " "
          concat arrow(column)
        end
      end
    end
    
    def arrow(column)
      return '' if params[:sort] != column
      arrow = params[:direction] == 'asc' ? 'down' : 'up'
      "<i class='fa fa-caret-#{arrow}'></i>".html_safe
    end
    

    show.html.erb

    <%= sortable "img_name", "Title" %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 2014-07-25
      • 1970-01-01
      • 2017-10-27
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多