首页 > 代码库 > yii2.0分页

yii2.0分页

本实例是对商品列表进行分页

1.Controller中,商品列表的方法actionList

  引用分页类

       

        actionList中:

        $goods_info=Goods::find()->joinWith(‘brand‘,‘category‘)->orderBy(‘goods.goods_id desc‘);
        $brand_model=new Brand();
        $brand_info=$brand_model::find()->all();
        $pages = new Pagination([‘totalCount‘=>$goods_info->count(),‘pageSize‘=>5]);
        $goods_info1=$goods_info->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render(‘list‘,[‘goods_info‘=>$goods_info1,‘brand_info‘=>$brand_info,‘brand_model‘=>$brand_model,‘pages‘ => $pages]);

2.视图页面list.php中

use yii\widgets\LinkPager;

 

 

 

 

<div class="text-right">
      <?= LinkPager::widget([‘pagination‘ => $pages]) ?>
</div> 

 

至此,yii2默认的分页已经完成,显示效果如图:

 

yii2.0分页