#
#
JavaScript
The helpers and paginators named *_js
require JavaScript support.
Simply add to your code the appropriate file(s) and statements as outlined below.
#
1. Choose the format that matches your app's configuration
#
2. Make the file available to your app
Depending on your app's architecture, you have a couple of options. Just pick one to uncomment in the pagy.rb initializer:
- For apps with an assets pipeline...
- Compatible with Propshaft, Importmaps, Sprockets, and similar tools.
Rails.application.config.assets.paths << Pagy::ROOT.join('javascripts')
- For apps with a builder...
- This works with builders like esbuild, Webpack, jsbundling-rails, etc.
# Example for Rails javascript_dir = Rails.root.join('app/javascripts') 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 event
window.addEventListener("yourEvent", Pagy.init)