yii分頁

控制器部分php

 

use yii\data\Pagination;

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 層
yii

 

use yii\widgets\LinkPager;


<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]);
?>

若是你不想要顯示上下頁,能夠將prevPageLabel和nextPageLabel設置爲false

默認不顯示首頁也尾頁,若是你須要,能夠這樣設置

若是你的數據過少,不夠2頁,默認不顯示分頁,若是你須要,設置便可

默認顯示的頁碼爲10頁,能夠設置maxButtonCount爲你想要展現的頁數
'maxButtonCount' => 5, 不想顯示頁數修改成 ,
<?= LinkPager::widget(['pagination' => $pages, 'nextPageLabel' => '下一頁', 'prevPageLabel' => '上一頁', ]); ?><?= LinkPager::widget(['pagination' => $pages, 'nextPageLabel' => false, 'prevPageLabel' => false, ]); ?><?= LinkPager::widget(['pagination' => $pages, 'firstPageLabel' => '首頁', 'lastPageLabel' => '尾頁', ]); ?>hideOnSinglePage=false'hideOnSinglePage' => false'maxButtonCount' => false

展現頁碼時修改一下就OK了ide

相關文章
相關標籤/搜索