控制器
引入
use yii\data\Pagination;
public function actionNews(){
$query = WebArticle::find()->where(['status' => 1]);
//总条数
$count = $query->count();
//pageSize是每页显示的条数
$pages = new Pagination(['totalCount' => $count,'pageSize' => '6']);
//当前页数
$page_num = (!empty($_GET['page']))?$_GET['page']:1;
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
//传参
return $this->renderPartial('newsInfo', [
'models' => $models,
'pages' => $pages,
'page_num' =>$page_num,
]);
}
视图view:
引入
use yii\widgets\LinkPager;
//循环遍历数据
= LinkPager::widget([
'pagination' => $pages,
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
]); ?>
1、不想显示上下页的话修改成 'nextPageLabel' => false, 'prevPageLabel' => false,
= LinkPager::widget([
'pagination' => $pages,
'nextPageLabel' => false,
'prevPageLabel' => false,
]); ?>
2、默认认不显示首页尾页,需要设置的话,这样设置
= LinkPager::widget([
'pagination' => $pages,
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
]); ?>
3、如果数据很少没达到两页的话,是不显示分页的,可以这样设置
= LinkPager::widget([
'pagination' => $pages,
'hideOnSinglePage' => false,
]); ?>
4、默认的是10页,想要设置多个页数可以这样设置
= LinkPager::widget([
'pagination' => $pages,
'maxButtonCount' => 10,
]); ?>
5、不喜欢默认的样式,可以自己设置样式
= LinkPager::widget([
'pagination' => $pages,
'options' => ['class' => 'm-pagination'],
]); ?>