ruby-on-rails-3 – 与kaminari的反向分页
发布时间:2020-12-16 20:23:59 所属栏目:百科 来源:网络整理
导读:我想为消息系统创建分页,其中显示的第一页包含最旧的消息,后续页面显示较新的消息. 例如,如果{a,b,c,d,e,f,g,h,i}每页正常分页数为3,则为: {a,c},{d,f},{g,i} 那么反向分页就是: {g,i},{a,c} 我打算预先加入页面,结果与正常的分页相同,只能从最后一页开始.
我想为消息系统创建分页,其中显示的第一页包含最旧的消息,后续页面显示较新的消息.
例如,如果{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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |