class Lucky::Paginator
- Lucky::Paginator
- Reference
- Object
Defined in:
lucky/paginator/paginator.crConstructors
Instance Method Summary
-
#first_page? : Bool
Returns
true
if the current#page
is the first one. - #full_path : String
- #item_count : Int32 | Int64
-
#item_range : Range
Returns the
Range
of items on this page. -
#last_page? : Bool
Returns
true
if current#page
is the last one. -
#next_page : Int32 | Nil
Returns the next page number or nil if the current page is the last one.
- #offset : Int32
-
#one_page? : Bool
Returns
true
if there is just one page. -
#overflowed? : Bool
Returns
true
if the current#page
is past the last page. -
#page : Int32
Returns the current page.
-
#path_to_next : String | Nil
Returns the path with a 'page' query param for the previous page.
-
#path_to_page(page_number : Int) : String
Generate a page with the 'page' query param set to the passed in
page_number
. -
#path_to_previous : String | Nil
Returns the path with a 'page' query param for the previous page.
- #per_page : Int32
-
#previous_page : Int32 | Nil
Returns the previous page number or nil if the current page is the first one.
-
#series(begin beginning : Int32 = 0, left_of_current : Int32 = 0, right_of_current : Int32 = 0, end ending : Int32 = 0) : Array(SeriesItem)
The
#series
method is smart and will not add gaps if there is no gap. -
#total : Int64
Returns the total number of pages.
Constructor Detail
Instance Method Detail
Returns the Range
of items on this page.
For example if you have 50 records, showing 20 per page, and you are on the 2nd page this method will return a range of 21-40.
You can get the beginning and end by calling begin
or end
on the
returned Range
.
Returns the next page number or nil if the current page is the last one.
Returns the current page. Return 1
if the passed in #page
is lower than 1
.
Returns the path with a 'page' query param for the previous page.
Return nil if there is no previous page
Generate a page with the 'page' query param set to the passed in page_number
.
Examples
pages = Paginator.new(
page: 1,
per_page: 25,
item_count: 70,
full_path: "/comments"
)
pages.path_to_page(2) # "/comments?page=2"
Returns the path with a 'page' query param for the previous page.
Return nil if there is no previous page
Returns the previous page number or nil if the current page is the first one.
The #series
method is smart and will not add gaps if there is no gap.
It will also not add items past the current page.
series = pages.series(begin: 6) series # [1, 2, 3, 4, 5]
As mentioned above the **actual** objects in the Array are made up of
`Lucky::Paginator::Gap`, `Lucky::Paginator::Page`, and
`Lucky::Paginator::CurrentPage` objects.
pages.series(begin: 1, end: 1)
Returns:
[
Lucky::Paginator::Page(1),
Lucky::Paginator::Gap,
Lucky::Paginator::CurrentPage(5),
Lucky::Paginator::Gap,
Lucky::Paginator::Page(10),
]
The `Page` and `CurrentPage` objects have a `number` and `path` method.
`Page#number` returns the number of the page as an Int. The `Page#path` method
Return the path to the next page.
The `Gap` object has no methods or instance variables. It is there to
represent a "gap" of pages.
These objects make it easy to use [method # overloading](https://crystal-lang.org/reference/syntax_and_semantics/overloading.html)
or `is_a?` to determine how to render each item.
Here's a quick example:
pages.series(begin: 1, end: 1).each do |item| case item when Lucky::Paginator::CurrentPage | Lucky::Paginator::Page pp! item.number # Int32 representing the page number pp! item.path # "/items?page=2" when Lucky::Paginator::Gap puts "..." end end
Or use method overloading. This will show an example using Lucky's HTML methods:
class PageNav < BaseComponent needs pages : Lucky::Paginator
def render pages.series(begin: 1, end: 1).each do |item| page_item(item) end end
def page_item(page : Lucky::Paginator::CurrentPage) # If it is the current page, just display text and no link text page.number end
def page_item(page : Lucky::Paginator::CurrentPage) a page.number, href: page.path end
def page_item(gap : Lucky::Paginator::Gap) text ".." end end