yii2.0 分页
PHP Code复制内容到剪贴板
- <?= \yii\widgets\LinkPager::widget([
- 'pagination' => $dataProvider->pagination,
- 'firstPageLabel' => '首页',
- 'lastPageLabel' => '尾页',
- 'nextPageLabel' => '下一页',
- 'prevPageLabel' => '上一页',
- 'maxButtonCount' => 5, // 默认显示的页码为10页
- 'hideOnSinglePage' => false, //不够2页,默认不显示分页
- 'prevPageCssClass' => 'previous',
- 'nextPageCssClass' => 'next',
- 'options' => ['class' => 'page', 'tag' => 'ul'],
- 'linkContainerOptions' => ['tag' => 'li']
- ]); ?>
GII生成,searchmodel:
控制器:
PHP Code复制内容到剪贴板
- public function actionIndex()
- {
- $model = new Tag();
- $searchModel = new TagSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('/index', [
- 'model' => $model,
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
PHP Code复制内容到剪贴板
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'pagination' => [
- 'pageSizeParam' => 'pageSize', /* 定义传输的每页显示变量名,默认为per-page */
- 'pageParam'=> 'pageNo' /* 定义传输的页码变量名,默认为page */
- ],
- 'sort' => [
- 'defaultOrder' => [
- 'id' => SORT_DESC
- ]
- ]
- ]);
view:
PHP Code复制内容到剪贴板
- use yii\grid\GridView;
- GridView::widget([
- 'dataProvider' => $dataProvider,
- 'filterModel' => $searchModel,
- 'columns' => [
- 'tag_id',
- 'tag_name',
- // 'link_plan_num',
- 'tag_style',
- [
- 'class' => 'app\widgets\grid\SwitcherColumn',
- 'attribute' => 'status'
- ],
- [
- 'class' => 'yii\grid\ActionColumn',
- 'header' => '操作',
- 'buttons' => [
- 'view' => function ($url, $model) {
- return Html::a(Html::icon('eye'), ['/admin/tag/view/', 'id' => $model->tag_id, 'mid' => Yii::$app->request->get("mid")], ['class' => 'btn btn-default btn-xs', 'title' => '查看']);
- },
- 'update' => function ($url, $model) {
- return Html::a(Html::icon('eyedropper'), ['/admin/tag/update/', 'id' => $model->tag_id, 'mid' => Yii::$app->request->get("mid")], ['class' => 'btn btn-default btn-xs', 'title' => '编辑']);
- },
- 'delete' => function ($url, $model) {
- return Html::a(Html::icon('trash'), ['/admin/tag/delete', 'id' => $model->tag_id, 'mid' => Yii::$app->request->get("mid")], ['class' => 'btn btn-default btn-xs', 'title' => '删除']);
- },
- ]
- ],
- ],
- ]);
在API中支持:
PHP Code复制内容到剪贴板
- $pageSize = request()->post("pageSize",10);
- $pageNo = request()->post("pageNo",1);
- $searchModel = new SupplierSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- $dataProvider->pagination = [
- 'pageSize' => $pageSize,
- 'page' => $pageNo
- ];
以下两种方式,search无效:
第一种:Activedataprovider 类分页的使用
PHP Code复制内容到剪贴板
- use yii\data\ActiveDataProvider;
- use mdu\tag\model\Tag;
- public function actionIndex()
- {
- $model = new Tag();
- $searchModel = new TagSearch();
- $dataProvider = new ActiveDataProvider([
- 'query' => $model->find(),
- 'pagination' => [
- 'pagesize' => '10',
- ]
- ]);
- return $this->render('/index', [
- 'model' => $model,
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
view代码
PHP Code复制内容到剪贴板
- use yii\grid\GridView;
- GridView::widget([
- 'dataProvider' => $dataProvider,
- 'filterModel' => $searchModel,
- 'columns' => [
- 'attribute',
- [
- 'attribute' => 'create_time',
- 'format' => ['data', 'Y-m-d H:i:s'],
- 'options' => ['width' => '200']
- ]],
- ['class' => 'yii\grid\ActionColumn', 'header' => '操作', 'headerOptions' => ['width' => '80']],
- ]);
第二种分页方法:
控制器:
PHP Code复制内容到剪贴板
- <?php
- use yii\data\Pagination;
- use app\models\Comment;
- public function actionComment(){
- $data = Comment::find()->andWhere(['id' => '10']);
- $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '2']);
- $model = $data->offset($pages->offset)->limit($pages->limit)->all();
- return $this->render('comment',[
- 'model' => $model,
- 'pages' => $pages,
- ]);
- }
- ?>
view:
PHP Code复制内容到剪贴板
- <?php
- use yii\widgets\LinkPager;
- ?>
- foreach($model as $key=>$val)
- {
- 这里就是遍历数据了,省略......
- }
- <?= LinkPager::widget(['pagination' => $pages]); ?>
不分页:
PHP Code复制内容到剪贴板
- $dataProvider = new ActiveDataProvider([
- 'query' => Problem::find()->where(['type' => 2]),
- 'pagination' => false
- ]);
控制层:
PHP Code复制内容到剪贴板
- use yii\data\Pagination;
- $pages = new Pagination(['totalCount' =>$query->count(), 'pageSize' => '10']);
- $model = $query->offset($pages->offset)->limit($pages->limit)->all();
视图层:
PHP Code复制内容到剪贴板
- <?php
- $request = Yii::$app->request;
- $currentPage = $request->get("page", 1);// 当前页
- $currentPage = $currentPage < 1 ? 1 : $currentPage; //页码小于1的时候,默认等于1
- $totalCount = $pages->totalCount; // 总个数
- $pageSize = $pages->pageSize; //每页显示个数
- $totalPage = ceil($totalCount / $pageSize); //总页数
- ?>
- <?php
- if ($totalPage > 1) {
- ?>
- <!-- 分页 -->
- <div class="zhishi-page">
- <!--总页数-->
- <span>共<?= $totalCount ?>条</span>
- <?php
- // 如果当前页码小于总页数,出现下一页
- if ($currentPage > 1) {
- ?>
- <a href="<?= Url::current(['page' => $currentPage - 1], true) ?>">上一页</a>
- <?
- }
- ?>
- <?php for ($i = 1; $i <= $totalPage; $i++): ?>
- <a href="<?= Url::current(['page' => $i], true) ?>"
- class="<?= $i ?> <?= $currentPage == $i ? "active" : "" ?>"><?= $i ?></a>
- <?php endfor; ?>
- <?php
- // 如果当前页码小于总页数,出现下一页
- if ($currentPage < $totalPage) {
- ?>
- <a href="<?= Url::current(['page' => $currentPage + 1], true) ?>">下一页</a>
- <?
- }
- ?>
- <span>共<?= $totalPage ?>页</span>
- </div>
- <?
- }
- ?>
PHP Code复制内容到剪贴板
- $query = Plan::find();
- $countQuery = clone $query;
- $pages = new Pagination(['totalCount' => $countQuery->count()]);
- $models = $query->offset($pages->offset)
- ->limit($pages->limit)
- ->asArray()
- ->all();
- $nodes = [];
- foreach ($models as $k => $v){
- $nodes[] = [
- "id" => $v["id"],
- "id" => $v["id"],
- ];
- }
- // echo "共".$pages->getPageCount()."页 "; //每页显示个数
- // echo "第".($pages->page + 1)."页 ";
- // echo "共".$pages->totalCount."条".PHP_EOL; // 总个数
- // echo "每页显示:".$pages->pageSize."条".PHP_EOL; //每页显示个数
- // p($models);