首页 > 技术文章 > YII2连表分页

hangxing1996 2017-05-02 14:43 原文

控制器(controller)页面

use \yii\data\Pagination;  //引入这个类

public function actionList(){

        $data = Clock::find()->select('*')->innerJoin('user','clock.user_id=user.id'); //联查  

        $pages = new Pagination(['totalCount'=>$data->count(),'pageSize'=>3]);

        $res = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all();

        return $this->render('list',['data'=>$res,'pages'=>$pages]);

}

视图(views)层

<?= yii\widgets\LinkPager::widget([

        'pagination' => $pages, 

        'nextPageLabel' => '下一页', 

        'prevPageLabel' => '上一页', 

        'firstPageLabel' => '首页', 

        'lastPageLabel' => '尾页'

]);?>

<div style="float:left;padding-left:400px;"><?= yii\widgets\LinkPager::widget([
'pagination' => $pages,
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页'
]);?></div>

推荐阅读