使用Kaminari进行反向分页

18

我想在一个消息系统中创建分页,其中第一页显示最旧的消息,随后的页面则显示更新的消息。

例如,如果正常的分页是对于{a,b,c,d,e,f,g,h,i}每页3个元素:

{a,b,c}, {d,e,f}, {g,h,i}

然后反向分页将是:

{g,h,i}, {d,e,f}, {a,b,c}

我计划在页面前添加内容,以使结果与正常分页相同,只是从最后一页开始。

使用kaminari可以实现吗?


1
为什么不直接对反转的数据集进行分页呢?YourModel.order("created_at ASC").page - thomasfedb
4个回答

3

Kaminary.paginate_array 不生成带有偏移和限制的查询条件。出于优化考虑,您不应该使用它。

相反,您可以这样做:

@messages = query_for_message.order('created_at DESC').page(params[:page]).per(3)

其中query_for_message代表用于检索分页记录的任何查询。例如,它可以是特定对话框中的所有消息。

现在在视图文件中,您只需要以相反的顺序显示@messages即可。例如:

<%= render :collection => @messages.reverse, :partial => 'message' %>
<%= paginate @messages %>

2

在Github上有一个很好的示例仓库,名为reverse_kaminari。它提供了一种实现方式,代码如下(源码)

class CitiesController < ApplicationController

  def index
    @cities = prepare_cities City.order('created_at DESC')
  end

  private

  def prepare_cities(scope)
    @per_page = City.default_per_page
    total_count = scope.count
    rest_count = total_count > @per_page ? (total_count % @per_page) : 0
    @num_pages = total_count > @per_page ? (total_count / @per_page) : 1

    if params[:page]
      offset = params[:page].sub(/-.*/, '').to_i
      current_page = @num_pages - (offset - 1) / @per_page
      scope.page(current_page).per(@per_page).padding(rest_count)
    else
      scope.page(1).per(@per_page + rest_count)
    end
  end

end

所有的功劳归功于Andrew Djoga。他还将应用程序托管为一个可工作的演示版


1

0

是的,但我想出的方法并不是很好看。实际上,你必须设置自己的顺序:

Message.page(1).per(3).order("created_at DESC").reverse!

这种方法存在两个问题:

首先,reverse! 调用将作用域解析为数组并执行查询,从而削弱了使用 AR scopes 的一些强大功能,这是第一个问题。

其次,与任何反向分页一样,您的偏移量将会移动,这意味着在两次重复调用之间,您可能会发送完全相同的数据,即使有3条新消息发送。这个问题是反向分页固有的问题。

另一种替代方法是询问“最后”页面编号,并将您的页面编号向下递增到1。


类似这样的代码:pages = Message.count % 3; Message.page(pages - i).per(3).order("created_at DESC") - trompa
如果你要采取这种方法,除了 order("created_at DESC") 外,应该将其更改为 order("created_at ASC") - Daniel Evans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接