Package org.javalite.activejdbc
Class Paginator<T extends Model>
java.lang.Object
org.javalite.activejdbc.Paginator<T>
- All Implemented Interfaces:
Serializable
This class supports pagination of result sets in ActiveJDBC. This is useful for paging through tables. If the
Model subclass is annotated with @
Cached, then this class will
cache the total count of records returned by getCount(), as LazyList will cache the result sets.
You can generate an instance each time you need one, or you can cache an instance in a session or even servlet context.- Author:
- Igor Polevoy
- See Also:
- Serialized Form
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classPaginator.PaginatorBuilder<T extends Model>Provides a builder pattern to create new instances of paginator. -
Constructor Summary
ConstructorsConstructorDescriptionPaginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)Convenience constructor.Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, String countQuery, Object... params)Paginator is created with parameters to jump to chunks of result sets (pages).Convenience constructor. -
Method Summary
Modifier and TypeMethodDescriptiongetCount()Returns total count of records based on provided criteria.intReturns index of current page, or 0 if this instance has not produced a page yet.longgetFrom()Returns index of the first item in a current page.booleangetNext()Synonym forhasNext().getPage()getPage(int pageNumber)This method will return a list of records for a specific page.intbooleanSynonym forhasPrevious().longgetTo()Returns index of the last item in a current page.booleanhasNext()booleanstatic <E extends Model>
Paginator.PaginatorBuilder<E>instance()Use to create a paginator instance, and provide arguments as needed.Use to set order by(s).longvoidsetCurrentPageIndex(int currentPageIndex, boolean skipCheck)Sets an index of a current page.
-
Constructor Details
-
Paginator
Convenience constructor. CallsPaginator(Class, int, String, Object...)and passes true forsuppressCounts. -
Paginator
public Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)Convenience constructor. CallsPaginator(Class, int, String, Object...)and passes null forcountQuery. -
Paginator
public Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, String countQuery, Object... params)Paginator is created with parameters to jump to chunks of result sets (pages). This class is useful "paging" through result on a user interface (web page).Examples of a sub-query:
"last_name like '%John%'"- this is a sub-query, and the rest of the information will be filled out by this class- "*" - will search for all records, no filtering
Full query example
- "select * from people where last_name like '%John%'"
- Parameters:
modelClass- model class mapped to a table.pageSize- number of items per page.suppressCounts- suppress calling "select count(*)... " on a table each time. If set to true, it will call count only once. If set to false, it will call count each timegetCount()is called fromhasNext()as well.params- a set of parameters if a query is parametrized (has question marks '?').query- this is a query that will be applied every time a new page is requested; this query should not contain limit, offset or order by clauses of any kind, Paginator will do this automatically. This parameter can have two forms, a sub-query or a full query.
-
-
Method Details
-
orderBy
Use to set order by(s). Example:"category, created_at desc"- Parameters:
orderBys- a comma-separated list of field names followed by either "desc" or "asc"- Returns:
- instance to self.
-
getPage
This method will return a list of records for a specific page.- Parameters:
pageNumber- page number to return. This is indexed at 1, not 0. Any value below 1 is illegal and will be rejected.- Returns:
- list of records that match a query make up a "page".
-
getCurrentPage
public int getCurrentPage()Returns index of current page, or 0 if this instance has not produced a page yet.- Returns:
- index of current page, or 0 if this instance has not produced a page yet.
-
getPrevious
public boolean getPrevious()Synonym forhasPrevious().- Returns:
- true if a previous page is available.
-
hasPrevious
public boolean hasPrevious() -
getNext
public boolean getNext()Synonym forhasNext().- Returns:
- true if a next page is available.
-
hasNext
public boolean hasNext() -
pageCount
public long pageCount()- Returns:
- a number of pages
-
getCount
Returns total count of records based on provided criteria.- Returns:
- total count of records based on provided criteria
-
getPageSize
public int getPageSize() -
instance
Use to create a paginator instance, and provide arguments as needed.- Returns:
- self.
-
setCurrentPageIndex
public void setCurrentPageIndex(int currentPageIndex, boolean skipCheck)Sets an index of a current page. This method will make a quick count query to check that the index you are setting is within the boundaries.- Parameters:
currentPageIndex- index of a current page.skipCheck-trueto skip the upper boundary check (will not make a call to DB).
-
getPage
- Returns:
- records for the current page.
-
getFrom
public long getFrom()Returns index of the first item in a current page. Use in UI where you need a message:Displaying 101 to 140 itemsir something similar.- Returns:
- index of the first item in a current page.
-
getTo
public long getTo()Returns index of the last item in a current page. Use in UI where you need a message:Displaying 101 to 140 itemsir something similar.- Returns:
- index of the last item in a current page.
-