使用 will_paginate 对多个分页页面进行排序

Sorting across multiple pagination pages with will_paginate

我目前正在使用 will_paginate 对 table 进行分页,也可以使用 jquery 进行排序。问题是分页只会传递 table 作为对象的当前可见 table,比如 100 个 table 项目中的 25 个。所以排序只发生在25的一页上。有没有办法将所有100项排序然后分页?

@people = @results.records.where(active: true).order('signup_at DESC NULLS LAST').paginate(page: params[:page], per_page: 25)

- if @people.any?
 %table.ui.sortable.striped.table
%thead
  %tr
    %th Name
    %th Email
    %th Phone
    %th Submissions
    %th Signed up
    %th Signup form
    %th Most recent note

%tbody
  = render @people

=分页@people

最简单的方法是将查询参数添加到允许不同排序键的端点:

/users/2/comments?sort_key=name
/users/2/comments?sort_key=age

然后在你的控制器中设置:

@people = @results.records.where(active: true).order("#{params['sort+key']} DESC NULLS LAST").paginate(page: params[:page], per_page: 25)

然后你得到了整组 100 条评论的按键排序。

你想达到什么目的,2 种排序策略在实现和用户体验方面确实令人困惑。要么全部获取并在 JavaScript 中实现分页,要么离开分页服务器端。这两种策略完全不同,一种是获取所有结果 (JavaScript),而另一种是分页,这意味着每次重新加载页面都需要一个查询 运行,命中数据库。

请注意,您仍然可以在当前集合中使用 JQuery 应用不同的排序(在本例中,您得到的 25 个结果)。