方法一:
1.view层(lists/show.html.erb)
< div id="comments">
< %= render :partial => ‘comments' %>
</div>
js文件中添加:
$(‘.pagination a’).attr(‘data-remote’, ‘true’); //指定分页为ajax请求
2.controller层(show)
def show
@comments=ZyComment.order("created_at desc").paginate :per_page=>5, :page=>params[:page]||1
respond_to do |format|
format.html
format.js #ajax分页
end
end
3.js template(文件show.js.erb)
注意:如果show文件中有其他js效果要在show.js.erb文件中重新加载一遍
$("#comments").html("<%=escape_javascript(render :partial=>'comments') %>");
$(".pagination a").attr("data-remote", "true");
4.will_paginate参数设置
<%=will_paginate @arrays, {
:previous_label => " 上一页 " , #这个一般是用在我们中文网站上的时辰须要进行批改,默认是 << Previous
:next_label => " 下一页 ", #这个一般是用在我们中文网站上的时辰须要进行批改,默认是 Next >>
:inner_window => 4, # inner_window? 把握显示当前页邻近的几许个链接 ,默认是4
:outer_window => 1 , # outer_window 把握显示首/末页邻近的几许个链接,默认是1
:page_links => false, # 若是是false的时辰,只显示上一页和下一页 (默认是 true)
:separator => "|", # 这个参数是用来设置页码之间 的分隔符的,用空格或者(|)或者其他的都可以
:param_name => :page, #这个参数是用来我们点击页码连接的时辰传递的参数的名称,一般不消批改
:class=>"pagination", #这个是用来给分页的元素家heml的类名的,可以经由过程这个类名进行样式布局。
:container=>true #默认值为true,是否添加外层div
}%>
方法二:
class RemoteLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
def prepare(collection, options, template)
@remote = options.delete(:remote) || { }
super
end
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target
# page_attr = { :page => target }
@template.link_to( text.to_s.html_safe, target, :remote => true )
end
end
will_paginate一些api:
total_pages 取总页数
current_page 当前页
total_entries 取总记录