ruby-on-rails-3 – 与kaminari的反向分页

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

例如,如果{a,b,c,d,e,f,g,h,i}每页正常分页数为3,则为:

{a,c},{d,f},{g,i}

那么反向分页就是:

{g,i},{a,c}

我打算预先加入页面,结果与正常的分页相同,只能从最后一页开始.

这是否可能与kaminari?

解决方法

Github上有一个很好的例子,在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.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...