控制器部分
use yiidataPagination;
public function actionCe() //分页测试
{
$test=new Hshuo(); //实例化model模型
$like=isset($_POST['like'])?$_POST['like']:''; //模糊查询的值
$arr=$test->find()->andFilterWhere(['like','content',"$like"]);
$pages = new Pagination([
//'totalCount' => $countQuery->count(),
'totalCount' => $arr->count(),
'pageSize' => 2 //每页显示条数
]);
$models = $arr->offset($pages->offset)
->limit($pages->limit)
->all();
// print_r($models);die;
return $this->render('ce', [
'models' => $models,
'pages' => $pages
]);
}
view 层
use yiiwidgetsLinkPager;
<center>
<form action="index.php?r=hshuo/ce" method="post">
<input type="text" name="like" placeholder="请输入关键字">
<input type="submit" value="搜索" >
</form>
<table border="1">
<tr>
<td>用户名</td>
<td>时间</td>
</tr>
<?php foreach($models as $v): ?>
<tr>
<td><?php echo $v['content'] ?></td>
<td><?php echo date("Y-m-d H:i:s",$v['time']); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
<?= LinkPager::widget(['pagination' => $pages,'firstPageLabel' => '首页', 'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页','lastPageLabel' => '尾页', 'maxButtonCount' => 5]);
?>
<?= LinkPager::widget(['pagination' => $pages, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', ]); ?>
如果你不想要显示上下页,可以将prevPageLabel和nextPageLabel设置为false
<?= LinkPager::widget(['pagination' => $pages, 'nextPageLabel' => false, 'prevPageLabel' => false, ]); ?>
默认不显示首页也尾页,如果你需要,可以这样设置
<?= LinkPager::widget(['pagination' => $pages, 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页', ]); ?>
如果你的数据过少,不够2页,默认不显示分页,如果你需要,设置hideOnSinglePage=false
即可
'hideOnSinglePage' => false
默认显示的页码为10页,可以设置maxButtonCount为你想要展示的页数
'maxButtonCount' => 5, 不想显示页数修改为 'maxButtonCount' => false
,
展示页码时修改一下就OK了