2016-04-15 12:25:29 9967次浏览 3条回答 1 悬赏 10 金钱

控制器代码:

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Country;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;

class CountryController extends Controller
{
	function actionIndex(){
		$query = Country::find()->where(['status' => 1]);
		$pages = new Pagination(['totalCount' => count($query), 'pageSize' => '1']);
		$models = $query->offset($pages->offset)
		    ->limit($pages->limit)
		    ->all();

		return $this->render('index', [
		     'models' => $models,
		     'pages' => $pages,
		]);
	}
}



?>

view

<?php
use yii\widgets\LinkPager;

foreach ($models as $model) {
    // 在这里显示 $model
    echo 'ID:'.$model->id;
    echo '名称:'.$model->name;
    echo '验证码:'.$model->code;
    echo '<hr/>';

}

?>

 <?php echo  LinkPager::widget(['pagination' => $pages]); ?>

数据库中有11条数据,这里我设置每页显示1条。应该有11页才对。
但是页面只显示foreach中的输出。
却没有看到上下页的分页样式。

最佳答案

  • 小诸葛 发布于 2016-04-15 12:45 举报
    Controller action:
    
    function actionIndex()
    {
        $query = Article::find()->where(['status' => 1]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
    
        return $this->render('index', [
             'models' => $models,
             'pages' => $pages,
        ]);
    }
    View:
    
    foreach ($models as $model) {
        // display $model here
    }
    
    // display pagination
    echo LinkPager::widget([
        'pagination' => $pages,
    ]);
    

    对比你的代码,你是怎么用的

您需要登录后才可以回答。登录 | 立即注册
菜鸟请教大神
实习生

菜鸟请教大神

注册时间:2016-04-15
最后登录:2016-04-15
在线时长:0小时14分
  • 粉丝0
  • 金钱0
  • 威望0
  • 积分0

热门问题