yii 组件分页

来源:互联网 发布:产学研合作协议 软件 编辑:程序博客网 时间:2024/05/17 07:42
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;
class CountryController extends Controller{
    public function actionIndex()    {      
      $query = Country::find();
      $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,
        ]);   

         }}



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

0 0
原创粉丝点击