yii2 分页函数

 后台代码:

  public function actionIndex()
    {
       $data=Yii::$app->request->get();
       
        $query=ExhibitionModel::find();


       $result = $this->getPagedRows($query, ['order' => 'create_time DESC', 'pageSize' => '5', 'rows' => 'list']);
        return $this->render('index',[
            'ehibition_model' => $result['list'],
            'pages' => $result['pages'],
        
        ]);
    }


    /**
     * 分页函数
     * @param integer $bid
     * @return mixed
     * @author  wufeng
     */
    public function getPagedRows($query, $config = [])
    {
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        if (isset($config['pageSize'])) {
            $pages->setPageSize($config['pageSize'], true);
        }

        $rows = $query->offset($pages->offset)->limit($pages->limit);
        if (isset($config['order'])) {
            $rows = $rows->orderBy($config['order']);
        }
        $rows = $rows->all();

        $rowsLable = 'rows';
        $pagesLable = 'pages';

        if (isset($config['rows'])) {
            $rowsLable = $config['rows'];
        }
        if (isset($config['pages'])) {
            $pagesLable = $config['pages'];
        }

        $ret = [];
        $ret[$rowsLable] = $rows;
        $ret[$pagesLable] = $pages;

        return $ret;
    }

前端代码     样式自己调整

use yii\widgets\LinkPager;

        <div class="page">
            总共<span><?=$pages->totalCount?></span>个项目
            <?= LinkPager::widget([
                'pagination' => $pages,
                'prevPageLabel'=>'上一页',
                'nextPageLabel'=>'下一页',
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
                'maxButtonCount'=>5,
                'options' =>['class' => 'pagination pagination-sm'],
//                'activePageCssClass'=>'page-item active',
//                'prevPageCssClass'=>'page-item',
                //'lastPageCssClass'=>'am-pagination-next',
//                'nextPageCssClass'=>'page-item',
                // 'disabledPageCssClass'=>'am-pagination-next',
//                'linkOptions'=>['class'=>'page-link'],
            ]); ?>
       </div>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值