Home>

I'm thinking of customizing pagination using kaminari in Ruby on Rails.
The current pagenation is below.

The customization I want to do will be displayed on 5 pages as shown below.

(Previous) (1) (2) (③) (4) ・ (10) (Next)
(Previous) (1) ・ (3) (④) (5) ・ (10) (Next)
* ○ is the display page.

If i have a solution here, I would appreciate it if you could teach me.
If i have any necessary files, please let us know.
Thank you.

Corresponding source code
<ul>
<li><% = paginate @admins%></li>
</ul>
def news
  @admins = Admin.order ("created_at DESC"). page (params [: page]). per (1)
end
ja:
  time: time:
    formats: formats:
      default: "% Y% m month% d day"
  views:
    pagination:
      previous: "previous"
      next: "Next"
      truncate: "・"
#frozen_string_literal: true
Kaminari.configure do | config |
  config.default_per_page = 8
  config.max_per_page = nil
  # config.window = 3
  # config.outer_window = 0
  # config.left = 0
  # config.right = 0
  # config.page_method_name =: page
  # config.param_name =: page
  # config.max_pages = nil
  # config.params_on_first_page = false
end
<% = paginator.render do%>
<nav role = "navigation" aria-label = "pager">
    <% = prev_page_tag%>
  <% each_page do | page |%>
    <% if page.display_tag?%>
      <% = page_tag page%>
    <% elsif! page.was_truncated?%>
      <% = gap_tag%>
    <% end%>
  <% end%>
  <% unless current_page.out_of_range?%>
    <% = next_page_tag%>
  <% end%>
</nav>
<% end%>
What I tried

I tried to touch views and config with reference to the article, but there was no change.

  • Answer # 1

    Refer to the article

    There is, but I think that it is better to get into the habit of seeing the formula as much as possible.


    It looks like the following in the default state without using a template.
    After that, I think that I should arrange the layout, translate it into Japanese, and delete First and Last.

    <% = paginate @admins, window: 1, outer_window: 1%>


    * I couldn't find a way to delete first and last, but if you're not particular about it, you may delete it with css.


    P.S.
    When the first page is displayed, it will look like the one below, so it seems necessary to take some measures such as making a conditional branch and calling the one with the outer increment or decrement.


    It is a further addition.

    Premise: With gem installed (kaminari config is not set, paginator is not customized)
    With the code belowAlmostIt can be realized. (Actually, it is better to write else properly)
    https://kaminari-test.herokuapp.com/

    <%
      n = 1
      n = 3 if @ pages.current_page == 1 || @ pages.current_page == @ pages.total_pages
      n = 2 if @ pages.current_page == 2 || @ pages.current_page == @ pages.total_pages --1
    %>
    <% = paginate @pages, window: n, outer_window: 1%>


    AlmostBecause when you display page 4,Prev 1 2 3 4 5 ... 25 NextIt will be. (* Same for page 22)
    If you skip two or more characters, it will be ..., but if you skip one character, it will not be ... It seems to be the specification of kaminari.

  • Answer # 2

    The settings here are still commented out, what happens if I comment them out?

    #frozen_string_literal: true
    Kaminari.configure do | config |
      config.default_per_page = 8
      config.max_per_page = nil
      config.window = 3 # Comment out here
      config.outer_window = 1 # Comment out here
      # config.left = 0
      # config.right = 0
      # config.page_method_name =: page
      # config.param_name =: page
      # config.max_pages = nil
      # config.params_on_first_page = false
    end