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.
Add page(params[:page]) in front get data query in controller function. 4. Add <%= paginate @attribute %> in html view and it is done. Kaminari automatically handles each and everything for it.
I wasn't able to get the answer above to work. What I did was to calculate the window size and pass it into my pagination partial.
window = @items.current_page < 5 ? 5 - @items.current_page : 1
render partial: 'pagination; locals: {items: @items, window: window}
.col-lg-6.pull-right
...
= paginate items, remote: true, window: window
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With