Kaminari pagination control with fixed page link count
kaminari, ruby-on-rails
Solution
You could monkey patch Kaminari's `Paginator` and `PageProxy` classes. By overriding methods like `relevant_pages`, `inside_window?`, `left_outer?`, and `right_outer?` you can control when a page link gets shown in the paginate view helper.
To get started, create a new file in `config/initializers` called `kaminari.rb` and paste in the following code:
module Kaminari
module Helpers
class Paginator < Tag
def relevant_pages(options)
1..options[:total_pages]
end
class PageProxy
def inside_window?
if @options[:current_page] <= @options[:window]
@page <= (@options[:window] * 2) + 1
elsif (@options[:total_pages] - @options[:current_page].number) < @options[:window]
@page >= (@options[:total_pages] - (@options[:window] * 2))
else
(@options[:current_page] - @page).abs <= @options[:window]
end
end
end
end
end
end
It's not pretty but it gets the job done. If you set `window: 5` in your view then this will always show a total of 10 links plus another `<span>` for the current page.
To learn more, check out the source code https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/helpers/paginator.rb
Problem
I would like to have Kaminari to show pagination links at fixed count with pagination control, for example 10 links on each navigation page. Kaminari default shows 6 page links at first page and the page links continue to grow when you continue browsing until you reach 9 items. I show in picture here, when I first load it will have 5 links in total. When I continue to browse, it will grow. Until you browse for the 5th link, it only show total links of 9. How do I consistently have a link count of 10 even at the beginning or the end of the navigation with Kaminari. I have try with Kaminari config.window, but that's not what I want.