分页
//控制器中
public function actionIndex()
{
$query=Country::find();//创建一个activequerry实例
$pagination=new Pagination([//返回一个分页条
'defaultPageSize'=>5,//每页显示五条
'totalCount'=>$query->count(),//总共页数
]);
$countries=$query->orderBy('name')
->offset($pagination->offset)//从第几条开始读
->limit($pagination->limit)//读几条
->all();
return $this->render('index',[
'countries'=>$countries,
'pagination'=>$pagination,
]);
}
View中
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach($countries as $country): ?>
<li>
<?=Html::encode("{$country->name} ({$country->code})")?>
<?=$country->population?>
</li>
<?php endforeach;?>
</ul>
<?=LinkPager::widget(['pagination'=>$pagination])?>