I was searching how to create pager in Yii2 using LinkPage widget.
Is there any example? I am new in Yii, so any help would be good.
I was searching how to create pager in Yii2 using LinkPage widget.
Is there any example? I am new in Yii, so any help would be good.
It is simple
$dataProvider = new ActiveDataProvider([
'query' => User::find(),
'pagination' => array('pageSize' => 50),
]);
echo \yii\widgets\LinkPager::widget([
'pagination'=>$dataProvider->pagination,
]);
Or if you don't use dataProvider you should use this:
$query = User::find();
$pagination = new Pagination(['totalCount' => $query->count(), 'pageSize'=>30]);
echo \yii\widgets\LinkPager::widget([
'pagination' => $pagination,
]);
In controller:
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,
]);
}
In view file:
foreach ($models as $model) {
// display $model here
}
// display pagination
echo LinkPager::widget([
'pagination' => $pages,
]);
Below is too simple for adding pagination,
We just need to add in controller,
$dataProvider = new ActiveDataProvider([
'query' => Post::find(),
'pagination' => [
'pageSize' => 20,
],
]);
Yii2 will bring pagination on index page, https://yii2-framework.readthedocs.io/en/stable/guide/output-data-widgets/
In your controller
$searchModel = new CourseModuleMasterSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination = ['pageSize' => 20];//add this line