A simple pagination API to paginate ActiveRecord and Mongoid models.
gem install neat-pages
or add the following line to Gemfile:
gem 'neat-pages'
First you need to link the assets.
In your css manifest put : require neat_pages
In your javascript manifest put : require neat_pages
You only need to require the javascript if you use the AJAX functionality.
That's it.
# app/controllers/products_controller.rb
def index
paginate
@products = Product.all.paginate(pagination)
end
<%# app/views/products/index.html.erb %>
<%= render 'products', products: @products %>
<%= neat_pages_navigation %>
# app/controllers/products_controller.rb
def index
paginate per_page: 50 # Default is 20
@products = Product.all.paginate(pagination)
end
<%# app/views/products/index.html.erb %>
<%= neat_pages_ajax_items 'products', products: @products %>
<%= neat_pages_navigation %>
Create the file app/views/products/index.neatpage.erb and place the following code in it.
<%# app/views/products/index.neatpage.erb %>
<% self.formats = ["html"] %>
<%= render 'products', products: @products %>
For every request that called the method paginate
, the header's response will contain the following data :
X-Total-Items=200;
X-Total-Pages=20;
X-Per-Page=10;
X-Current-Page=3;
If the page requested is out of bound, the controller method render_out_of_bound
will be called.
This method will render the text 'out_of_bound' with a status 404. If you want, you can override it like this :
# app/controllers/application_controller.rb
def render_out_of_bound
render text: 'My custom code', status: 404
end
neat_pages_ajax_items(partial_path, options={})
Activate the ajax pagination. See the previous example.
This will wrap the content of your partial in a div by default. If your items are in a table
you can pass the parameter wrapper: 'tbody'
. So the call would look like that :
<%# app/views/products/index.html.erb %>
<table>
<%= neat_pages_ajax_items 'products', products: @products, wrapper: 'tbody' %>
</table>
<%= neat_pages_navigation %>
neat_pages_more_button
Generate a more button that will lazy load the records instead of showing a navigation like (< 1 2 3 4 >).
neat_pages_navigation
Generate a page navigation. (ex: < 1 2 3 4 >)
neat_pages_link_relation_tags
Put this line in the head tag to generate the link relation tags
neat_pages_status
Generate the current state of the pagination (ex: 30 to 40 / 200)
Triggered on 'body'
whenever a page has changed, from cache or from server.
The event data is an object literal which holds those fields:
noPage
The current page number
Triggered on 'body'
when there's no more record to load. Use with the more button.
If you want to translate the text in the pagination helpers, just add the following keys in i18n.
fr:
neat_pages:
next_page: Page suivante
previous_page: Page précédente
more: Plus de produits
over: Aucun autre produit de disponible
Copyright (c) 2013 De Marque inc. See LICENSE for further details.