Yii2.0自定义分页方法

Yii2.0的分页方法有两种,

第一种是调用自带的分页组件,以及渲染组件,

第二则是半调用,仍然调用分页组件但可以自己渲染views页面

方法一:使用ActiveDataProvider 和 GridView

public function actionIndex(){
        $dataProvider = new ActiveDataProvider([
            'query' => ArticleClass::find(),
            'pagination' => [
                'pagesize' => '2',
             ]
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
}

只需要在action中添加pagination属性指明pagesize参数即可,view使用GridView,效果如下


方法二:控制器中引入分页类,并在views中引入分页渲染

controller中:

use yii\data\Pagination;


public function actionIndex(){
        $query = ArticleClass::find();
        $countQuery = clone $query;
        $pageSize = 2;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => $pageSize]);
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('index', [
            'models' => $models,
            'pages' => $pages,
        ]);
}

view中:

use yii\widgets\LinkPager;


".$model['class_name']."";
        }
?>
    
     $pages, 
        'nextPageLabel' => '下一页', 
        'prevPageLabel' => '上一页', 
        'firstPageLabel' => '首页', 
        'lastPageLabel' => '尾页',
    ]); ?>

效果如下:



你可能感兴趣的:(YII框架)