When data needs to be rendered in multiple pages, Pagination can be used to
represent information such as [[totalCount|total item count]], [[pageSize|page size]],
[[page|current page]], etc. These information can be passed to [[yii\widgets\Pager|pagers]]
to render pagination buttons or links.
The following example shows how to create a pagination object and feed it
to a pager.
Controller action:
~~~
function actionIndex()
{
$query = Article::find()->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index', [
'models' => $models,
'pages' => $pages,
]);
}
~~~
View:
~~~
foreach ($models as $model) {
display $model here
}
display pagination
echo LinkPager::widget([
'pagination' => $pages,
]);
~~~
Returns the zero-based current page number.
public getPage ( boolean $recalculate = false ) : integer |
$recalculate |
boolean |
whether to recalculate the current page based on the page size and item count. |
return |
integer |
the zero-based current page number. |
getPageCount()
public method
getPageSize()
public method
By default, this method will try to determine the page size by [[pageSizeParam]] in [[params]].
If the page size cannot be determined this way, [[defaultPageSize]] will be returned.
public getPageSize ( ) : integer |
return |
integer |
the number of items per page. If it is less than 1, it means the page size is infinite,
and thus a single page contains all items. |
Sets the current page number.
public setPage ( integer $value, boolean $validatePage = false ) |
$value |
integer |
the zero-based index of the current page. |
$validatePage |
boolean |
whether to validate the page number. Note that in order
to validate the page number, both [[validatePage]] and this parameter must be true. |
setPageSize()
public method
$defaultPageSize public_oe property
the default page size. This property will be returned by [[pageSize]] when page size
cannot be determined by [[pageSizeParam]] from [[params]].
$forcePageParam public_oe property
whether to always have the page parameter in the URL created by
Pagination::createUrl.
If false and [[page]] is 0, the page parameter will not be put in the URL.
$pageParam public_oe property
name of the parameter storing the current page index.
$pageSizeLimit public_oe property
the page size limits. The first array element stands for the minimal page size, and the second
the maximal page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]].
$pageSizeParam public_oe property
name of the parameter storing the page size.
$validatePage public_oe property
whether to check if [[page]] is within valid range.
When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1).
Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available
in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page
number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]].