如何在Kaminari中获取显示范围

How to get the displayed range in Kaminari

我需要自定义 _pagination.html.erb 以显示与 page_entries_info 辅助方法相同的信息,例如:显示第 6 到 10 条记录,共 26 条。

有这样的_pagination.html.erb

<%= paginator.render do -%>

<% end -%>

有哪些方法或对象可用于获取当前显示的范围(上例中为 6 到 10),以及正在分页的记录总数(本例中为 26)?

第一种方法,您可以覆盖 Kaminari 本地化以显示格式 "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}"

第二种方法,随心所欲_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' %>