如何用will_paginate gem实现ajax分页
发布时间:2020-12-16 03:10:17 所属栏目:百科 来源:网络整理
导读:我在我的ROR项目中使用will_paginate gem来显示页面中的记录。 我想要下载页面,而不是使用ajax重新加载整个页面。 我在网上发现了一些例子,但对我来说并不奏效。 怎么办? 创建一个新的帮手(例如app / helpers / will_paginate_helper.rb),其中包含以下内
我在我的ROR项目中使用will_paginate gem来显示页面中的记录。
我想要下载页面,而不是使用ajax重新加载整个页面。 我在网上发现了一些例子,但对我来说并不奏效。 怎么办?
创建一个新的帮手(例如app / helpers / will_paginate_helper.rb),其中包含以下内容:
module WillPaginateHelper class WillPaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer def prepare(collection,options,template) options[:params] ||= {} options[:params]['_'] = nil super(collection,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分页: <%= js_will_paginate @recipes %> 请记住,分页链接将包括URL的现有参数,您可以排除以下内容。这是标准的分页功能: <%= js_will_paginate @recipes,:params => { :my_excluded_param => nil } %> 希望能解决你的问题。 更新1:添加了一个解释,这是如何工作的original question我发布这个解决方案。 更新2:我已经使Rails 4与remote:true链接兼容,并将帮助方法重命名为js_will_paginate。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |