美文网首页
列表的搜索分页

列表的搜索分页

作者: meteorites | 来源:发表于2017-07-17 00:15 被阅读0次

    后端代码

    public function actionList()
        {
        //接收查询的关键字
        $keywords = Yii::$app->request->get('keywords',"");
        
        //获取页码,参数需和Pagination中配置的参数一致
        $page=Yii::$app->request->get('page');
        $page=($page-1)>0?($page-1):0;
        $pageSize=5;
        
        $goods = Goods::find()
            ->where(['status'=>1])
            ->offset($page*$pageSize)
            ->limit($pageSize)
            ->orderBy('id');
        
        //判断查询是否为空
        if (!empty($keywords)) {
            $goods->where("name like '%{$keywords}%' OR goods_no like '%{$keywords}%'");
        }
        
        //设置分页配置
        $config=[
            'totalCount' => $goods->count(),
            'defaultPageSize' => $pageSize,
        ];
        
        //实例化分页器,将配置参数传入到Pagination中
        $pages = new Pagination($config);
        
        $categoryModel = GoodsCategory::find()->asArray()->all();
        $brands = Brand::find()->asArray()->all();
        $goods = $goods->all();
        
        return $this->render('list',[
            'category'=>$categoryModel,
            'brands' =>$brands,
            'goods' => $goods,
            'pages' => $pages
            ]);
        }
    

    前端代码

    <tr>
    <td align="right" nowrap="true" colspan="6">
        <div id="turn-page">
            <?=\yii\widgets\LinkPager::widget([
                    'pagination' => $pages,
                    'nextPageLabel' => '下一页',
                    'prevPageLabel' => '上一页',
                    'firstPageLabel' => '首页',
                    'lastPageLabel' => '尾页',
                    'maxButtonCount' => 5,
                    'hideOnSinglePage' => false,
            ])?>
        </div>
    </td>
    </tr>
    

    相关文章

      网友评论

          本文标题:列表的搜索分页

          本文链接:https://www.haomeiwen.com/subject/djoxkxtx.html