5

Say I have 30 pages being paginated with Kaminari:

#800 books, 25 per page
=paginate @books, :outer_window => 3, :window => 3

It looks like this:

1 2 3 4 … 30 31 32

There's no way to get to page 16 without clicking 5 times.

  1. 1* 2 3 [ 4 ] … 30 31 32
  2. 1 2 3 4* 5 6 [ 7 ] … 30 31 32
  3. 1 2 3 4 5 6 7* 8 9 [ 10 ] … 30 31 32
  4. 1 2 3 … 7 8 9 10* 11 12 [ 13 ] … 30 31 32
  5. 1 2 3 … 10 11 12 13* 14 15 [ 16 ] … 30 31 32

(* current page, [] = click)

It seems like a lot of work to get to the middle of the stack.

  1. How can I extend Kaminari to go to page 16 when clicking on the gap (...) ?
  2. Is it possible to extend Kaminari to show the middle from the start?

1 2 3 4 ... 15 16 17 ... 30 31 32

Ashbury
  • 2,160
  • 3
  • 27
  • 52
  • Did you try this [section of readme](https://github.com/amatsuda/kaminari#customizing-the-pagination-helper)? I don't have any project using kaminari handy but I think once you have the views it should be pretty straightforward to customise them – Mike Szyndel Jan 18 '16 at 11:03
  • I generated the views but there doesn't seem to be any methods to get what page is before or after the gap. You can do num_page/2 like @kacz says, but that only works if you're on the first page. When you have two gaps I'd love to see what page is linked before and after the gap, then you could do some easy arithmetic to get the midpoint. – Ashbury Jan 22 '16 at 07:48

2 Answers2

1

You can play with the inner and outer window options (link) but I don't think this will solve your problem.

As @Michal Szyndel's comment mentioned you will need to use the generator (rails g kaminari:views THEME note: that you can pass -e to generate different templating languages). Once you have generated the files you can edit the "paginator" partial with some custom logic. For an intro to how to do this watch Ryan's railscast (starting at 4:53)

Hopefully this gets you far enough because how that middle window works when you aren't at the beginning or end of your pagination will depend on you window sizes and what you want. (e.g., If you have an outer window of 3 and an inner window of 2 with 50 pages what should the paginator links look like when you are on page 15?)

If you need more help just reply with a comment and I'll answer more specifically.

John Kacz
  • 404
  • 5
  • 15
1

Extending the kaminari view elements is pretty simple. You need to customize the pagination helper, https://github.com/amatsuda/kaminari#customizing-the-pagination-helper

Specifically, and assuming you are using ERB, you'll want to edit your new app/views/kaminari/_gap.html.erb file.

<%
  url,data = url.split('?')
  url += '?page=' + (num_pages/2)
%>
<span class="page gap">
  <%= link_to_unless page.current?, page, url %>
</span>

This creates a link to your middle (really num_pages/2-th) page.

Ray Baxter
  • 3,181
  • 23
  • 27
  • How did you get url in the gap view? Also this will always jump to the middle, regardless of where the gap tag is. Sometimes there are two gap tags (1 2 3 4 ... 15 16 17 ... 30 31 32), where it wouldn't make sense to click on 4 '...' 15 and end up at page 16. – Ashbury Jan 23 '16 at 21:01
  • The url is constructed from the existing url and adds a page parameter that points to the middle of the gap. That's on lines 2 and 3. What page would you want to go to if you clicked on the ellipsis between 4 and 5? If you want to cover that case as well, you'll need to open up the `app/views/kaminari/ _paginator.html.erb` and on this line https://github.com/amatsuda/kaminari/blob/master/app/views/kaminari/_paginator.html.erb#L14 remove the `|| page.inside_window?`, move that to it's own special `if` clause and handle left and right gaps separately. – Ray Baxter Jan 25 '16 at 00:08