Yii 2.0 Pagination Demo based on MVC
Pagination is a normal task in Web Dev. Today will show how to build standard MVC pagination in Yii 2.0 Framework.
{ Controller }
//get total topic count
$query = Topic::find()->where(['t_status_id'=>Topic::STATUS_PUBLISH]);
$count = $query->count();
//pagination
$pagination = new Pagination(['totalCount'=>$count, 'pageSize'=>20]);
//load page level topics
$query = Topic::find()->where(['t_status_id'=>Topic::STATUS_PUBLISH]);
$topics = $query->offset($pagination->offset)
->limit($pagination->limit)
->orderBy('t_id DESC')
->all();
return $this->render('index', [
'topics' => $topics,
'pagination' => $pagination
]);
{ View }
<?php foreach ($topics as $oneTopic):?>
//TODO render topic detail
<?php endforeach;?>
<!-- Start Pagination -->
<?= LinkPager::widget([
'pagination' => $pagination,
'options'=>['class'=>'leePage pagination'],
]);; ?>
<!-- End Pagination -->
Leave Comment