thinkphp移动端分页修改方案

发布时间:2019-04-29 23:34:20 浏览量:1419 标签: bootstrap thinkphp PHP

tihnkphp框架的分页方法在每一个开发程序中都会遇到,框架中的分页存在两个问题:一、样式没有默认;二、分页非响应式。上期我们说了tihnkphp分页样式,感兴趣的可以前往查看。


今天主要说下tihnkphp框架移动端分页样式如何调整;


timg.jpg

一、使用方式


在thinkphp框架核心文件中找到library->think->paginator->driver->Bootstrap.php文件,将其打开在文件69行位置,添加PC和移动端判断方式,演示如下:

//true 需要自行判断
if(true){
    //手机端分页
}else{
    //PC端分页
}



二、示例代码:

//手机端分页
$side   = 3;
$window = $side * 2;

if ($this->lastPage < $window + 6) {
$block['first'] = $this->getUrlRange(1, $this->lastPage);
} else if ($this->currentPage <= $window - 4) {
//首页
$block['first'] = $this->getUrlRange(1, $window - 3);
$block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
} else if ($this->currentPage > ($this->lastPage - $window + 4)) {
//尾页
$block['first'] = $this->getUrlRange(1, 2);
$block['last']  = $this->getUrlRange($this->lastPage - ($window - 4), $this->lastPage);
} else {
//中页
$block['first']  = $this->getUrlRange(1, 2);
$block['slider'] = $this->getUrlRange($this->currentPage, $this->currentPage);
$block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
}


三、整个代码文件

/**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        if ($this->simple) {
            return '';
        }

        $block = [
            'first'  => null,
            'slider' => null,
            'last'   => null,
        ];

        $side   = 3;
        $window = $side * 2;

        if(true){
            //手机端分页
            if ($this->lastPage < $window + 6) {
            $block['first'] = $this->getUrlRange(1, $this->lastPage);
            } else if ($this->currentPage <= $window - 4) {
            //首页
            $block['first'] = $this->getUrlRange(1, $window - 3);
            $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
            } else if ($this->currentPage > ($this->lastPage - $window + 4)) {
            //尾页
            $block['first'] = $this->getUrlRange(1, 2);
            $block['last']  = $this->getUrlRange($this->lastPage - ($window - 4), $this->lastPage);
            } else {
            //中页
            $block['first']  = $this->getUrlRange(1, 2);
            $block['slider'] = $this->getUrlRange($this->currentPage, $this->currentPage);
            $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
            }
        }else{
            //PC端分页
            if ($this->lastPage < $window + 6) {
                $block['first'] = $this->getUrlRange(1, $this->lastPage);
            } elseif ($this->currentPage <= $window) {
                $block['first'] = $this->getUrlRange(1, $window + 2);
                $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
            } elseif ($this->currentPage > ($this->lastPage - $window)) {
                $block['first'] = $this->getUrlRange(1, 2);
                $block['last']  = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
            } else {
                $block['first']  = $this->getUrlRange(1, 2);
                $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
                $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
            }
        }
        
        $html = '';

        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['last']);
        }

        return $html;
    }



评论
登录后才可以进行评论哦! QQ登录
验证码
评论内容