will_paginate 排序 - 某些列默认 ASC,某些列默认 DESC
我在控制器中有以下内容来启用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用默认排序定义哈希,从那里获取非设置排序:
Define a hash with the default sortings, grab the non-set sorting from there:
想通了!
这是来自 Railscasts #228 和 #240 的原始
application_helper.rb
代码。我将其更改为(第 1 行和第 4 行的更改):
这使我现在可以在列标题中指定默认方向:
Figured it out!
This was the original
application_helper.rb
code from Railscasts #228 and #240.I changed it to (changes on lines 1 and 4):
This enables me to now specify a default direction in a column heading: