Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to the midde with Kaminari pagination

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

like image 277
Ashbury Avatar asked Oct 30 '22 11:10

Ashbury


2 Answers

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.

like image 63
John Kacz Avatar answered Nov 08 '22 05:11

John Kacz


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.

like image 23
Ray Baxter Avatar answered Nov 08 '22 06:11

Ray Baxter