will_paginate 排序 - 某些列默认 ASC,某些列默认 DESC

发布于 2024-11-17 13:03:13 字数 494 浏览 6 评论 0原文

我在控制器中有以下内容来启用 will_paginate 排序:

def sort_column
  ['name', 'scheduled', 'status'].include?(params[:sort]) ? params[:sort] : "scheduled"
end
def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

这些由表列顶部的 <%= sortable "status", "Status" %> 补充 - 单击此按钮将排序桌子。

如何使某些列默认为 ASC,某些列默认为 DESC?

基本原理

基于文本的列在 ASC 排序下效果很好。但是,当我有包含百分比或数字的列时,有时我想首先显示最佳/最高的值,而不是要求用户单击两次以按照他们想要的方式对列进行排序。

I have the following in a controller to enable will_paginate sorting:

def sort_column
  ['name', 'scheduled', 'status'].include?(params[:sort]) ? params[:sort] : "scheduled"
end
def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

These are supplemented by <%= sortable "status", "Status" %> at the top of a table column - clicking this will sort the table.

How can I make some columns default to ASC and some default to DESC?

Rationale

A text-based column works very well under ASC sorting. However, where I have columns with percentages or numbers, sometimes I want to show the best/highest first instead of requiring that the user click twice to get the column sorted the way they want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2024-11-24 13:03:13

使用默认排序定义哈希,从那里获取非设置排序:

def sort_direction
  sort_defaults = { 'name' => 'asc', 'age' => 'desc', 'scheduled' => 'asc' }
  sort_defaults.has_value?(params[:direction]) ? params[:direction] : sort_defaults[sort_column]
end

Define a hash with the default sortings, grab the non-set sorting from there:

def sort_direction
  sort_defaults = { 'name' => 'asc', 'age' => 'desc', 'scheduled' => 'asc' }
  sort_defaults.has_value?(params[:direction]) ? params[:direction] : sort_defaults[sort_column]
end
生生漫 2024-11-24 13:03:13

想通了!

这是来自 Railscasts #228 和 #240 的原始 application_helper.rb 代码。

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

我将其更改为(第 1 行和第 4 行的更改):

def sortable(column, title = nil, direction = 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, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end

这使我现在可以在列标题中指定默认方向:

<div class="col> 
  <%= sortable "priority_number", "Priority", "desc" %> 
</div>

Figured it out!

This was the original application_helper.rb code from Railscasts #228 and #240.

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

I changed it to (changes on lines 1 and 4):

def sortable(column, title = nil, direction = 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, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end

This enables me to now specify a default direction in a column heading:

<div class="col> 
  <%= sortable "priority_number", "Priority", "desc" %> 
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文