yii2 实现分页

// Controller

public function actionList() {
        $this->layout = 'right';
        $article = new Article();
        $query = $article::find();
        $count = $query->count();

  // 每页显示的数量(可在配置文件中定义)
        $pageSize = 4;

  // 实例分页类冰传入数据总条数和每页显示的数量
        $page = new Pagination(['totalCount' => $count, 'pageSize' =>$pageSize]);

  // 设置offset和limit
        $models = $query->offset($page->offset)->limit($page->limit)->all();
        return $this->render('arc_list', [
            'page' => $page,
            'models' => $models
        ]);
    }

// View

<?php
        foreach ($models as $key => $value) {
            echo '...';
        }
        ?>

        <?= \yii\widgets\LinkPager::widget([
            'pagination' => $page,
            'firstPageLabel' => '首页',
            'prevPageLabel' => '上一页',
            'nextPageLabel' => '下一页',
            'lastPageLabel' => '尾页',
            'maxButtonCount' => 1,  // 最大按钮显示条数
            'optinons' => ['class' => 'my style']   // 自定义分页样式
            ]);
?>

猜你喜欢

转载自www.cnblogs.com/AI-geek/p/10502853.html
今日推荐