Skip to content

Instantly share code, notes, and snippets.

@zvlex
Forked from maximum-pixels/paginate_helper.rb
Created April 7, 2016 10:20
Show Gist options
  • Save zvlex/50ff5c6b0bd7bdc811484e9186f8f71e to your computer and use it in GitHub Desktop.
Save zvlex/50ff5c6b0bd7bdc811484e9186f8f71e to your computer and use it in GitHub Desktop.
This is a custom link renderer that will format the pagination bar with Bootstrap4 as well as AJAX (i.e. data-remote="true") to use jquery-ujs rails
module PaginateHelper
class PaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer
def prepare(collection, options, template)
options[:params] ||= {}
options[:params]['_'] = nil
super(collection, options, template)
end
protected
def html_container(html)
tag(:nav, tag(:ul, html, class: "pagination"))
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
tag(:li, tag(:a, text, class: classname), class: "disabled")
end
end
def page_number(page)
unless page == current_page
tag(:li, link(page, page, :rel => rel_value(page)))
else
tag(:li, tag(:a, page), class: "active")
end
end
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
tag(:li, @template.link_to(text.to_s.html_safe, target, attributes.merge(remote: true)))
end
def gap
tag(:li, tag(:a, '...'))
end
end##end of class
def js_will_paginate(collection, options = {})
will_paginate(collection, options.merge(:renderer => PaginateHelper::PaginateJSLinkRenderer))
end
end
<%= js_will_paginate @posts, {page: params[:page], per_page: 30} %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment