如何实现Ajax分页与will_paginate gem是项目 [英] How to Implement ajax pagination with will_paginate gem
问题描述
我使用 will_paginate
宝石在我的 ROR
的项目,以显示在页面的记录。
I am using will_paginate
gem in my ROR
project to show the records in pages.
我想下一个页面,而无需使用 AJAX
。
I want the next page to be loaded without reloading the whole page using ajax
.
我发现在网络上的一些例子,但他们不为我工作。
I found some examples on the net but they don't work for me.
如何做到这一点?
推荐答案
与下面的内容创建一个新的辅助(如应用程序/佣工/ will_paginate_helper.rb。):
Create a new helper (ex. app/helpers/will_paginate_helper.rb) with the following content:
module WillPaginateHelper
class WillPaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer
def prepare(collection, options, template)
options[:params] ||= {}
options[:params]['_'] = nil
super(collection, options, template)
end
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
@template.link_to(target, attributes.merge(remote: true)) do
text.to_s.html_safe
end
end
end
def js_will_paginate(collection, options = {})
will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer))
end
end
然后在您的视图中使用此标记为Ajax分页:
Then in your view use this tag for ajax pagination:
<%= js_will_paginate @recipes %>
记住,分页链接包括URL的现有PARAMS,您可以排除这些如下图所示。这是标准的将分页功能:
Remember that the pagination links will include existing params of the url, you can exclude these as shown below. This is standard will paginate functionality:
<%= js_will_paginate @recipes, :params => { :my_excluded_param => nil } %>
希望能解决你的问题。
Hope that solves your problem.
更新1 :添加了这是如何工作的<一个一个解释href="http://stackoverflow.com/questions/13787431/ajax-call-in-rails-3-2-3-with-will-paginate-gem/13975268">original问题,我张贴了这个解决方案。
Update 1: Added a explanation of how this works to the original question where I posted this solution.
更新2 :我找到了轨道4与远程兼容:真正的联系,并改名为辅助方法的 js_will_paginate
Update 2: I've made it Rails 4 compatible with remote: true links and renamed the helper method to js_will_paginate.
这篇关于如何实现Ajax分页与will_paginate gem是项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!