will_paginate - 高度具体的排序

发布于 2024-11-17 05:41:45 字数 489 浏览 6 评论 0原文

我有一个 priority 字段,其中有四个选项:“紧急”、“高”、“正常”和“低”。

我将此字段显示在记录表中,并且我正在使用 will_paginate 的排序,如下所示:

@projects = @company.projects.paginate :page => params[:page], :order => (sort_column + " " + sort_direction)

private
def sort_column
  ['priority', 'name'].include?(params[:sort]) ? params[:sort] : "priority"
end

上面的典型排序将给出:

  1. 高低
  2. 正常
  3. 紧急
  4. 此顺序是错误

的。我如何才能将此列专门排序为“紧急”、“高”、“正常”、“低”?

I have a priority field with four options: "Urgent", "High", "Normal", and "Low".

I have this field displayed in a table of records, and I am using will_paginate's ordering as follows:

@projects = @company.projects.paginate :page => params[:page], :order => (sort_column + " " + sort_direction)

private
def sort_column
  ['priority', 'name'].include?(params[:sort]) ? params[:sort] : "priority"
end

A typical ordering as above will give me:

  1. High
  2. Low
  3. Normal
  4. Urgent

This order is wrong. How can I get this column to order specifically to Urgent, High, Normal, Low?

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

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

发布评论

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

评论(3

阳光下慵懒的猫 2024-11-24 05:41:45

不确定 Rails 是否有更简单的方法来做到这一点,但我通常看到的方法是使用一个单独的表来保存您的优先级以及一个整数的订单字段。然后,您只需加入该表并按整数字段排序即可。

Not sure if Rails has an easier way to do this, but the way I typically see it done is to have a separate table holding your priorities along with an order field that's just an integer. Then you just join to that table and order by the integer field.

⒈起吃苦の倖褔 2024-11-24 05:41:45

我建议您看一下这个截屏视频。

它对数据列进行排序非常容易实施。

希望对您有帮助。

I would like you suggest to take a look at this screencast.

It does the sort data columns very easy to implement.

Hope it helps you.

任谁 2024-11-24 05:41:45

是的,您的数据库正在按字典顺序对优先级进行排序。最简单的解决方案是将这些字符串优先级映射到整数优先级并对其进行排序 - 正如 @jdc 建议的那样。

我可能倾向于避免将优先级存储在单独的表中,这样您就不必进行联接,而只需将数字优先级存储在新列中。

但想法是一样的。

Yes, your DB is sorting the priority in lexicographic order. The easiest solution is to map those string priorities to integer priorities and sort on that - as @jdc suggested.

I'd probably tend to stay away from storing the priorities in a separate table just so you dont have to do a join, and just store the numerical priority in a new column.

But the idea is the same.

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