【问题标题】:Reverse pagination with kaminari使用 kaminari 进行反向分页
【发布时间】:2012-11-24 03:40:37
【问题描述】:

我想为消息传递系统创建分页,其中显示的第一页包含最旧的消息,后续页面显示较新的消息。

例如,如果{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 可以做到这一点吗?

【问题讨论】:

  • 为什么不简单地对反向数据集进行分页? YourModel.order("created_at ASC").page ?

标签: ruby-on-rails-3 pagination kaminari


【解决方案1】:

在 Github 上有一个很好的示例 repo,在 github 上称为 reverse_kaminari。它建议按照这些思路实现(Source)

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。他还将应用程序托管为a working demo

【讨论】:

    【解决方案2】:

    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 %>
    

    【讨论】:

      【解决方案3】:

      解决此问题的一种方法是: Reverse pagination with kaminari? 它看起来不是很干净也不是最理想的,但它可以工作:)

      【讨论】:

        【解决方案4】:

        是的,但我想出的方法并不完全漂亮。实际上,您必须设置自己的顺序:

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

        这种方法的问题是双重的:

        首先反过来! call 将作用域解析为数组并执行查询,使用 AR 作用域削弱 kaminari 的一些令人敬畏的方面。

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

        另一种方法是询问“最后”页码并将您的页码向下递增到 1。

        【讨论】:

        • 类似:pages = Message.count % 3; Message.page(pages - i).per(3).order("created_at DESC")?
        • 如果您打算采用这种方法,则除了 order("created_at DESC") 应该是 order("created_at ASC")
        猜你喜欢
        • 2013-01-10
        • 2012-02-04
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-16
        • 2011-09-26
        • 1970-01-01
        相关资源
        最近更新 更多