网上关于will_paginate支持ajax的文章不少,可是看了一下,都比较旧了,在最新更新的will_paginate插件下用不了,所以就自己改了一下
在ViewHelper.rb中
@@pagination_options = {
:class => 'pagination',
:prev_label => '« Previous',
:next_label => 'Next »',
:inner_window => 4, # links around the current page
:outer_window => 1, # links around beginning and end
:separator => ' ', # single space is friendly to spiders and non-graphic browsers
:param_name => :page,
:params => nil,
:renderer => 'WillPaginate::LinkRenderer',
:page_links => true,
:container => true
}
增加update 修改为:
@@pagination_options = {
:class => 'pagination',
:prev_label => '« Previous',
:next_label => 'Next »',
:inner_window => 4, # links around the current page
:outer_window => 1, # links around beginning and end
:separator => ' ', # single space is friendly to spiders and non-graphic browsers
:param_name => :page,
:params => nil,
:renderer => 'WillPaginate::LinkRenderer',
:page_links => true,
:container => true,
:update => nil
}
page_link_or_span方法修改为:
def page_link_or_span(page, span_class = 'current', text = nil)
text ||= page.to_s
if page and page != current_page
if(@options[:update] != nil)
@template.link_to_remote text, :url=>url_options(page),:update=>@options[:update]
else
@template.link_to text, url_options(page)
end
else
@template.content_tag :span, text, :class => span_class
end
end
在页面上:普通链接方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label => '上一页',:next_label => '下一页'}%>
ajax方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label => '上一页',:next_label => '下一页',:update => 'content'}%>