Comments (7)
Kimchoy's method worked perfectly for me, but was spitting out escaped html entities into my layout, so I made the following change (plus a small change to the markup and hyphenation for my purposes):
def page_entries_info(collection, options = {})
collection_name = options[:collection_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.num_pages < 2
case collection.size
when 0; info = "No #{collection_name.pluralize} found"
when 1; info = "Displaying <strong>1</strong> #{collection_name}"
else; info = "Displaying <strong>all #{collection.size}</strong> #{collection_name.pluralize}"
end
else
info = %{Displaying #{collection_name.pluralize} <strong>%d–%d</strong> of <strong>%d</strong> in total}% [
collection.offset_value + 1,
collection.offset_value + collection.length,
collection.total_count
]
end
info.html_safe
end
from kaminari.
#page_entries_info
feature has been added a long time ago so I'm closing this issue. Thanks guys for the suggestion!
from kaminari.
I stumbled across the same issue; did not find anything in the source, so I threw together my own helper method to get somewhat similar results. The method is far from complete, and not terribly clean, but you are welcome to use it to work from.
def page_entries_info(collection)
content_tag :div, :class => "page_info" do
collection_name = collection.klass.name.downcase.pluralize
if collection.count > 0
"Displaying #{collection_name} #{collection.offset_value + 1} - #{collection.offset_value + collection.length} of #{collection.count} in total"
end
end
end
That said, given kaminari's focus as an engine with overridable views, this type of thing would be better served with a view along the lines of what paginate is doing, yeah?
from kaminari.
Thanks for this helper, it helps! :)
Btw,
collection.count
always returned 0, so I changed it to:
collection.klass.count
from kaminari.
i stumbled upon the same issue too, so this is what i did (reusing will_paginate method)
def page_entries_info(collection, options = {})
collection_name = options[:collection_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.num_pages < 2
case collection.size
when 0; "No #{collection_name.pluralize} found"
when 1; "Displaying <strong>1</strong> #{collection_name}"
else; "Displaying <b>all #{collection.size}</b> #{collection_name.pluralize}"
end
else
%{Displaying #{collection_name.pluralize} <strong>%d - %d</strong> of <strong>%d</strong> in total}% [
collection.offset_value + 1,
collection.offset_value + collection.length,
collection.total_count
]
end
end
from kaminari.
oh yeah. good idea cobychapple, saves the trouble of doing the html_safe on the views.
from kaminari.
For using this with Sunspot search results you need to tweak it slightly as offset_value is not defined
page_entries_info(@search.results, {:collection_name => 'result'})
def page_entries_info(collection, options = {})
collection_name = options[:collection_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.num_pages < 2
case collection.size
when 0; info = "No #{collection_name.pluralize} found"
when 1; info = "Displaying <strong>1</strong> #{collection_name}"
else; info = "Displaying <strong>all #{collection.size}</strong> #{collection_name.pluralize}"
end
else
offset_value = collection.per_page * (collection.current_page - 1)
info = %{Displaying #{collection_name.pluralize} <strong>%d–%d</strong> of <strong>%d</strong> in total}% [
offset_value + 1,
offset_value + collection.length,
collection.total_count
]
end
info.html_safe
end
from kaminari.
Related Issues (20)
- enter custom page number HOT 3
- rails g kaminari:views doesn't generate anything HOT 12
- Hi HOT 1
- undefined local variable or method `paginator' for _page.html.erb:22 partial HOT 2
- Undefined method `without_count' HOT 2
- Update wiki link in GitHub "About" section HOT 1
- issue on method_missing due to separation of positional and keyword arguments in Ruby 3
- Is it possible to add 'data-turbo-action' to the paginate helper? HOT 4
- How to use "Creating Friendly URLs and Caching" in "member do" ?
- With the update from i18n to v1.11.0 `:zero` in not longer supported as key
- Problem Monkey Patching Rails 7.0
- render error for js.erb
- when large table query, use total_count and total_pages slow
- paginate view helper is failing with includes HOT 3
- Updating content_for blocks
- `link_to_first_page` / `link_to_last_page` ?
- inconsistent results in case of LEFT OUTER JOIN, GROUP BY and HAVING HOT 2
- Array doesn't get paginated when total count is passed in HOT 1
- Ambiguous column "id" when PG COUNT is used with certain joins HOT 1
- Pagination links incorrect with Rails 7.1.3 and Kaminari 1.2.2 HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kaminari.