TP5扩展Paginate分页

浏览:5442 发布日期:2018/04/29 分类:用法示例 关键字: paginate 分页
扩展ThinkPHP5分页功能
Tp5分页好像没有显示首页、尾页和分页详细信息的设置,就扩展了一下。在application\extend目录下创建Pager.php文件:<?php
namespace app\extend;
use think\paginator\driver\Bootstrap;

class Pager extends Bootstrap {
    protected $params = [
        'home'  =>  '首页',
        'end'   =>  '尾页',
        'pre'   =>  '',  
        'next'  =>  '',
        'show'  =>  false //是否显示为自定义分页信息,默认不显示
    ];
    public function setParams(array $params=[]){
        foreach($params as $key=>$item){
            if(array_key_exists($key, $this->params)){
                $this->params[$key] = $item;
            }
        }
        return $this;
    }

    protected function getPreviousButton($text = "«") {
        $text = empty($this->params['pre']) ? $text : $this->params['pre'];
        return parent::getPreviousButton($text); // TODO: Change the autogenerated stub
    }

    protected function getNextButton($text = '»') {
        $text = empty($this->params['next']) ? $text : $this->params['next'];
        return parent::getNextButton($text); // TODO: Change the autogenerated stub
    }
    /**
     * 获取 共 多少 页信息
     * @return string
     */
    protected function getTotalInfo()
    {
        return '<li><span>共<strong>' . $this->lastPage() .'</strong>页 <strong>'.$this->total().'</strong> 条记录</span></li>';
    }

    protected function getFirstPage(){
        if($this->currentPage != 1){
            return '<li><a href="' . $this->url(1) . '">' . $this->params['home'] . '</a></li>';
        }else{
            return '<li><span>' . $this->params['home'] . '</span></li>';
        }
    }

    protected function getLastPage(){
        if($this->currentPage != $this->lastPage){
            return '<li><a href="' . $this->url($this->lastPage) . '">' . $this->params['end'] . '</a></li>';
        }else{
            return '<li><span>' . $this->params['end'] . '</span></li>';
        }

    }

    /**
     * 渲染分页html
     * @return mixed
     */
    public function render()
    {
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '<ul class="pager">%s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } elseif ($this->params['show']){
                return sprintf(
                    '<ul class="pagination">%s %s %s %s %s %s</ul>',
                    $this->getFirstPage(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->getLastPage(),
                    $this->getTotalInfo()
                );
            } else {
                return sprintf(
                    '<ul class="pagination">%s %s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }

}
在控制器中调用:$list = Db::connect('dbConfig')->table('goods')->where('id', '<=', 50000)->paginate(10, false, [
            'type'  =>  '\app\extend\Pager',
]);
$list->setParams([
            'show'  =>  true,
            'pre'   =>  '上一页',
            'next'  =>  '下一页',
]);
效果图:
评论( 相关
后面还有条评论,点击查看>>