码迷,mamicode.com
首页 > 其他好文 > 详细

yii2做输入跳转到分页

时间:2016-04-07 20:30:26      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

首先使用引用分页类
1、
1 use yii\data\Pagination;
2、控制器
 
 1 //分页跳转
 2     public function actionPagego(){
 3         $model =  Knowledge::find();
 4         //原生sql语句使用分页
 5         $sql = "SELECT k.id,k.title,k.copyfrom,k.username,k.updatetime FROM bl_knowledge k LEFT JOIN bl_kr kr ON k.id=kr.knowledgeid LEFT JOIN bl_res r ON kr.resourcesid=r.id WHERE k.status=1 GROUP BY k.id ORDER BY k.listorder desc, k.id ASC";
 6         $list = Yii::$app->db->createCommand($sql)->queryAll();
 7         $pagination = new Pagination([
 8             ‘defaultPageSize‘=>2,
 9             ‘totalCount‘=>count($list),
10         ]);
11         $counts  = count($list);
12         $all_pages =  ceil($counts/10);
13         $display_pagego = 1;
14         $result = Yii::$app->db->createCommand($sql." limit ".$pagination->limit." offset ".$pagination->offset."")->queryAll();
15  
16         return $this->renderPartial(‘index‘,[
17             ‘model‘         =>          $model,
18             ‘result‘        =>          $result,
19             ‘counts‘         =>          $counts,
20             ‘pagination‘    =>          $pagination,
21             ‘all_pages‘     =>          $all_pages,
22             ‘display_pagego‘=>          $display_pagego,
23         ]);
24     }
4、页面调用当前控制器,方法的时候,一定要获取到要跳转的页码数
   
1 //分页跳转
2     $("#tiaozhuango").click(function(){
3         var tiaozhuan = $(".tiaozhuan").val();
4         if(tiaozhuan){
5             location.href="/index.php?r=overseasknow/pagego&page="+tiaozhuan;
6         }else{
7             location.href="/index.php?r=overseasknow/index";
8         }
9     }); 
5、注意:传递的参数一定是要是“page”  因为,yii2中的分页类,传递page的数量是要跳转的页码
参考:yii\data\Pagination 文件
技术分享
 

yii2做输入跳转到分页

标签:

原文地址:http://www.cnblogs.com/wamp/p/5365125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!