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,
]);
~~~