首先使用引用分页类
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 文件
时间: 2024-10-13 12:20:32