前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >thinkphp移动端分页修改方案

thinkphp移动端分页修改方案

作者头像
申霖
发布2019-12-27 17:43:27
1.3K0
发布2019-12-27 17:43:27
举报
文章被收录于专栏:小白程序猿小白程序猿

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

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

一、使用方式

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

代码语言:javascript
复制
//true 需要自行判断
if(true){
    //手机端分页
}else{
    //PC端分页
}

二、示例代码:

代码语言:javascript
复制
//手机端分页
$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);
}

三、整个代码文件

代码语言:javascript
复制
/**
     * 页码按钮
     * @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;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档