#
#
JavaScript
The helpers and paginators named *_js*
require Pagy JavaScript support.
Simply add the appropriate file(s) and statements as outlined below.
#
1. Choose the format that matches your app's configuration
pagy.mjs
ES6 module for buiders like webpacker, esbuild, parcel, etc.pagy.min.js
A compact (~2.6k) minified IIFE filepagy.js
A plain IIFE file, ideal for debugging, accompanied bypagy.js.map
pagy.js.map
A source map file designed for debugging purposespagy.d.ts
Pagy TypeScript type definitions for advanced integration or customization
#
2. Make the file available to your app
Depending on your app's architecture, you have a couple of options:
- Add the Pagy JavaScript path to the assets path
- Compatible with Propshaft, Importmaps, Sprockets, and similar tools.
Rails.application.config.assets.paths << Pagy::ROOT.join('javascript')
- Alternatively, uncomment/edit the following lines in the pagy.rb initializer
- This works with builders like esbuild, Webpack, jsbundling-rails, etc.
# Example for Rails javascript_dir = Rails.root.join('app/javascript') Pagy.sync_javascript(javascript_dir, 'pagy.mjs') if Rails.env.development?
- Load it like any other JavaScript file or module you already use in your app
#
3. Set up the Pagy.init
Listener
The primary purpose of the JavaScript support is to utilize Pagy.init
as an event listener attached to a suitable window
event:
// Plain JavaScript
window.addEventListener("load", Pagy.init)
// Turbo
window.addEventListener("turbo:load", Pagy.init)
// Turbolinks
window.addEventListener("turbolinks:load", Pagy.init)
// Custom listener
window.addEventListener("yourEventListener", Pagy.init)