© 2017-2019 Domizio DemichelisMIT License

➡ Chat Support on Gitter ➡

Extras

Pagy comes with a few optional extensions/extras:

Extra Description Links
array Paginate arrays efficiently avoiding expensive array-wrapping and without overriding array.rb, documentation
bootstrap Add nav, nav_js and combo_nav_js helpers for the Bootstrap pagination component bootstrap.rb, documentation
bulma Add nav, nav_js and combo_nav_js helpers for the Bulma pagination component bulma.rb, documentation
countless Paginate without any count, saving one query per rendering countless.rb, documentation
elasticsearch_rails Paginate elasticsearch_rails gem results efficiently elasticsearch_rails.rb, documentation
foundation Add nav, nav_js and combo_nav_js helpers for the Foundation pagination component foundation.rb, documentation
headers Add RFC-8288 compliant http response headers (and other helpers) useful for API pagination headers.rb, documentation
i18n Use the I18n gem instead of the pagy implementation i18n.rb, documentation
items Allow the client to request a custom number of items per page with a ready to use selector UI items.rb, documentation
materialize Add nav, nav_js and combo_nav_js helpers for the Materialize CSS pagination component materialize.rb, documentation
metadata Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc. metadata.rb, documentation
navs Add nav_js and combo_nav_js javascript unstyled helpers navs.rb, documentation
overflow Allow for easy handling of overflowing pages overflow.rb, documentation
searchkick Paginate arrays efficiently avoiding expensive array-wrapping and without overriding searchkick.rb, documentation
semantic Add nav, nav_js and combo_nav_js helpers for the Semantic UI CSS pagination component semantic.rb, documentation
support Extra support for features like: incremental, infinite, auto-scroll pagination support.rb, documentation
trim Remove the page=1 param from links trim.rb, documentation
uikit Add nav, nav_js and combo_nav_js helpers for the UIkit pagination component uikit.rb, documentation

Synopsis

Extras are not loaded by default, so you should require them explicitly in your pagy.rb initializer (see Configuration):

require 'pagy/extras/bootstrap'
require 'pagy/extras/...'

Description

Extras don’t define any new module or class, they just re-open the Pagy class and modules, adding the extra methods as they were part of the loaded pagy gem. This neatly separates the core code from the optional extras, still keeping its usage as simple as it were part of the core.

Methods

A few extras require the pagy/extras/shared file. It defines only a few methods used internally, so you don’t actually need to use them directly.

Notice: All the other added methods are documented in the respective extras doc.

Javascript

A few helpers use javascript, and they are clearly recognizable by the js suffix:

If you use any of them you should load the pagy.js file, and run Pagy.init() on window load and eventually on AJAX-load.

CAVEATS

Add the oj gem

Although it’s not a requirement, if you are on ruby 2.0+ (not jruby), and if you use any *_nav_js helper, you should add the gem 'oj' to your Gemfile. When available, Pagy will automatically use it to boost the performance. (Notice: It does nothing for normal, non-js helpers.)

In rails apps

With the asset pipeline

If your app uses the sprocket asset-pipeline, add the assets-path in the pagy.rb initializer:

Rails.application.config.assets.paths << Pagy.root.join('javascripts')

Add the pagy javascript to the application.js:

//= require pagy

Add an event listener for turbolinks:

window.addEventListener("turbolinks:load", Pagy.init);

or a generic one if your app doesn’t use turbolinks:

window.addEventListener("load", Pagy.init);

With Webpacker

If your app uses Webpacker, ensure that the webpacker erb loader is installed:

bundle exec rails webpacker:install:erb

Then create a pagy.js.erb in order to render the content of pagy.js and add the event listener into it:

<%= Pagy.root.join('javascripts', 'pagy.js').read %>
window.addEventListener("load", Pagy.init)

and import it in app/javascript/application.js:

import '../src/javascripts/pagy.js.erb'

Notice:

In non-rails apps

Ensure the pagy/extras/javascripts/pagy.js script gets served with the page.

Add an event listener like:

window.addEventListener('load', Pagy.init);

or execute the Pagy.init() using jQuery:

$( window ).load(function() {
  Pagy.init()
});

Using AJAX with javascript-enabled helpers

If you AJAX-render any of the javascript helpers mentioned above, you should also execute Pagy.init(container_element); in the javascript template. Here is an example for a pagy_bootstrap_nav_js AJAX-render:

pagy_remote_nav_js controller action (notice the link_extra to enable AJAX):

def pagy_remote_nav_js
  @pagy, @records = pagy(Product.all, link_extra: 'data-remote="true"')
end

pagy_remote_nav_js.html.erb template for non-AJAX render (first page-load):

<div id="container">
  <%= render partial: 'nav_js' %>
</div>

_nav_js.html.erb partial shared for AJAX and non-AJAX rendering:

<%== pagy_bootstrap_nav_js(@pagy) %>

pagy_remote_nav_js.js.erb javascript template used for AJAX:

$('#container').html("<%= j(render 'nav_js')%>");
Pagy.init(document.getElementById('container'));

Notice the document.getElementById('container') argument: that will re-init the pagy elements just AJAX-rendered in the container div.