How to get the displayed range in Kaminari

199 Views Asked by At

I need to customize _pagination.html.erb to display the same information as the page_entries_info helper method, for example: Showing records 6 to 10 of 26.

Having a _pagination.html.erb like this:

<%= paginator.render do -%>

<% end -%>

What methods or objects are available to get the currently displayed range (6 to 10 in the example above), and the total number of records being paginated (26, in the example)?

1

There are 1 best solutions below

2
Lam Phan On BEST ANSWER

First approach, you could override Kaminari localize to display format "Showing %{entry_name} %{first} to %{last} of %{total}"

# config/locales/kaminari.yml
en:
 ...
 
   helpers:
    page_entries_info:
      entry:
        zero: "entries"
        one: "entry"
        other: "entries"
      one_page:
        display_entries:
          zero: "No %{entry_name} found"
          one: "Showing <b>1</b> %{entry_name}"
          other: "Showing <b>all %{count}</b> %{entry_name}"
      more_pages:
        display_entries: "Showing %{entry_name} %{first} to %{last} of %{total}"

Second approach, as you want to custom _pagination.html.erb

# app/views/kaminari/_pagination.html.erb
<%= paginator.render do -%>
  <% to = current_page.to_i * per_page %>
  <% from = to - per_page + 1 %>
  <% total = total_pages * per_page - 1 %>
  Showing <%= @options[:entry_name].pluralize(total, I18n.locale) %> <%= from %> to <%= to %> of <%= total %>
 
...

<% end %>

# you need to pass an option `entry_name`
# app/views/records/index.html.erb
<%= paginate @records, entry_name: 'record' %>