后台代码:
public function actionIndex()
{
$data=Yii::$app->request->get();
$query=ExhibitionModel::find();
$result = $this->getPagedRows($query, ['order' => 'create_time DESC', 'pageSize' => '5', 'rows' => 'list']);
return $this->render('index',[
'ehibition_model' => $result['list'],
'pages' => $result['pages'],
]);
}
/**
* 分页函数
* @param integer $bid
* @return mixed
* @author wufeng
*/
public function getPagedRows($query, $config = [])
{
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
if (isset($config['pageSize'])) {
$pages->setPageSize($config['pageSize'], true);
}
$rows = $query->offset($pages->offset)->limit($pages->limit);
if (isset($config['order'])) {
$rows = $rows->orderBy($config['order']);
}
$rows = $rows->all();
$rowsLable = 'rows';
$pagesLable = 'pages';
if (isset($config['rows'])) {
$rowsLable = $config['rows'];
}
if (isset($config['pages'])) {
$pagesLable = $config['pages'];
}
$ret = [];
$ret[$rowsLable] = $rows;
$ret[$pagesLable] = $pages;
return $ret;
}
前端代码 样式自己调整
use yii\widgets\LinkPager;
<div class="page">
总共<span><?=$pages->totalCount?></span>个项目
<?= LinkPager::widget([
'pagination' => $pages,
'prevPageLabel'=>'上一页',
'nextPageLabel'=>'下一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'maxButtonCount'=>5,
'options' =>['class' => 'pagination pagination-sm'],
// 'activePageCssClass'=>'page-item active',
// 'prevPageCssClass'=>'page-item',
//'lastPageCssClass'=>'am-pagination-next',
// 'nextPageCssClass'=>'page-item',
// 'disabledPageCssClass'=>'am-pagination-next',
// 'linkOptions'=>['class'=>'page-link'],
]); ?>
</div>