Yii中的分页

  控制器层

<?php
namespace frontend\controllers;

use frontend\models\Goods;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;

class HelloController extends Controller
{
public function actionIndex()
{
$query = Goods::find();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(),'defaultPageSize'=>2]);//此处需要引用Pagination,defaultPageSize为每页显示数据数量
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index', [
'models' => $models,
'pages' => $pages,
]);
}
}




view层
<?php
use yii\widgets\LinkPager;
?>
<table>
<?php foreach ($models as $model):?>
<tr>
<td><?php echo $model['g_name'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
echo LinkPager::widget([
'pagination' => $pages,
]);
posted @ 2017-03-17 18:50  武小帅  阅读(162)  评论(0编辑  收藏  举报