返回
热门搜索

【兼容移动端】ThinkPHP6.0(paginate)方法分页按钮页码数量修改

IT博客 后端开发 php 点击量 1622

基本信息 收藏 - 举报 - 海报

详细介绍

ThinkPHP6.0模板采用的分页样式驱动是Bootstrap的,给我们预定义了paginate分页类,帮助我们快速分页,但是ThinkPHP6提供的分页按钮数量太多了,拼成一团在巴掌大的手机屏上显示得别扭!两种方案解决问题:

1、采用AJAX滚动屏幕触底请求更多数据插入到列表页DIV底部。目前很多大型网站都采用这种方案。但是自己要做好SEO优化收录问题,因为搜索引擎抓取数据时可不能去做AJAX请求更多。可能会导致更多内容不能被收录。只能借助搜索引擎快速键接提交API接口来实现提交文章内容保存同时后台自动提交网址给收录。

2、修改内核分页代码,实现传统分页按钮方式。

具体的路径在vendor/topthink/think-orm/src/paginator/driver/Bootstrap.php,并在此基础上进行修改。

  $side   = 3; //分页代码数量控制代码,改为1

  1. <?php

  2. class Bootstrap extends Paginator{

  3.    /**

  4.     * 上一页按钮

  5.     * @param string $text

  6.     * @return string

  7.     */

  8.    protected function getPreviousButton(string $text = "&laquo;"): string    {

  9.        if ($this->currentPage() getDisabledTextWrapper($text);

  10.        }

  11.        $url = $this->url(

  12.            $this->currentPage() - 1

  13.        );

  14.        return $this->getPageLinkWrapper($url, $text);

  15.    }

  16.    /**

  17.     * 下一页按钮

  18.     * @param string $text

  19.     * @return string

  20.     */

  21.    protected function getNextButton(string $text = '&raquo;'): string    {

  22.        if (!$this->hasMore) {

  23.            return $this->getDisabledTextWrapper($text);

  24.        }

  25.        $url = $this->url($this->currentPage() + 1);

  26.        return $this->getPageLinkWrapper($url, $text);

  27.    }

  28.    /**

  29.     * 页码按钮

  30.     * @return string

  31.     */

  32.    protected function getLinks(): string    {

  33.        if ($this->simple) {

  34.            return '';

  35.        }

  36.        $block = [

  37.            'first'  => null,

  38.            'slider' => null,

  39.            'last'   => null,

  40.        ];

  41.        $side   = 3; //分页代码数量控制代码,改为1

  42.        $window = $side * 2;

  43.        if ($this->lastPage < $window + 6) {

  44.            $block['first'] = $this->getUrlRange(1, $this->lastPage);

  45.        } elseif ($this->currentPage getUrlRange(1, $window + 2);

  46.            $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);

  47.        } elseif ($this->currentPage > ($this->lastPage - $window)) {

  48.            $block['first'] = $this->getUrlRange(1, 2);

  49.            $block['last']  = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);

  50.        } else {

  51.            $block['first']  = $this->getUrlRange(1, 2);

  52.            $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);

  53.            $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);

  54.        }

  55.        $html = '';

  56.        if (is_array($block['first'])) {

  57.            $html .= $this->getUrlLinks($block['first']);

  58.        }

  59.        if (is_array($block['slider'])) {

  60.            $html .= $this->getDots();

  61.            $html .= $this->getUrlLinks($block['slider']);

  62.        }

  63.        if (is_array($block['last'])) {

  64.            $html .= $this->getDots();

  65.            $html .= $this->getUrlLinks($block['last']);

  66.        }

  67.        return $html;

  68.    }

  69.    /**

  70.     * 渲染分页html

  71.     * @return mixed

  72.     */

  73.    public function render()

  74.    {

  75.        if ($this->hasPages()) {

  76.            if ($this->simple) {

  77.                return sprintf(

  78.                    '%s %s',

  79.                    $this->getPreviousButton(),

  80.                    $this->getNextButton()

  81.                );

  82.            } else {

  83.                return sprintf(

  84.                    '%s %s %s',

  85.                    $this->getPreviousButton(),

  86.                    $this->getLinks(),

  87.                    $this->getNextButton()

  88.                );

  89.            }

  90.        }

  91.    }

  92.    /**

  93.     * 生成一个可点击的按钮

  94.     *

  95.     * @param  string $url

  96.     * @param  string $page

  97.     * @return string

  98.     */

  99.    protected function getAvailablePageWrapper(string $url, string $page): string    {

  100.        return '' . $page . '';

  101.    }

  102.    /**

  103.     * 生成一个禁用的按钮

  104.     *

  105.     * @param  string $text

  106.     * @return string

  107.     */

  108.    protected function getDisabledTextWrapper(string $text): string    {

  109.        return '' . $text . '';

  110.    }

  111.    /**

  112.     * 生成一个激活的按钮

  113.     *

  114.     * @param  string $text

  115.     * @return string

  116.     */

  117.    protected function getActivePageWrapper(string $text): string    {

  118.        return '' . $text . '';

  119.    }

  120.    /**

  121.     * 生成省略号按钮

  122.     *

  123.     * @return string

  124.     */

  125.    protected function getDots(): string    {

  126.        return $this->getDisabledTextWrapper('...');

  127.    }

  128.    /**

  129.     * 批量生成页码按钮.

  130.     *

  131.     * @param  array $urls

  132.     * @return string

  133.     */

  134.    protected function getUrlLinks(array $urls): string    {

  135.        $html = '';

  136.        foreach ($urls as $page => $url) {

  137.            $html .= $this->getPageLinkWrapper($url, $page);

  138.        }

  139.        return $html;

  140.    }

  141.    /**

  142.     * 生成普通页码按钮

  143.     *

  144.     * @param  string $url

  145.     * @param  string    $page

  146.     * @return string

  147.     */

  148.    protected function getPageLinkWrapper(string $url, string $page): string    {

  149.        if ($this->currentPage() == $page) {

  150.            return $this->getActivePageWrapper($page);

  151.        }

  152.        return $this->getAvailablePageWrapper($url, $page);

  153.    }}

  154. 例如简单修改上一页下一页为中文汉字,修改以下地方。

  155.  /**

  156.     * 上一页按钮

  157.     * @param string $text

  158.     * @return string

  159.     */

  160.    protected function getPreviousButton(string $text = "上一页"): string    {

  161.        if ($this->currentPage() getDisabledTextWrapper($text);

  162.        }

  163.        $url = $this->url(

  164.            $this->currentPage() - 1

  165.        );

  166.        return $this->getPageLinkWrapper($url, $text);

  167.    }

  168.    /**

  169.     * 下一页按钮

  170.     * @param string $text

  171.     * @return string

  172.     */

  173.    protected function getNextButton(string $text = '下一页'): string    {

  174.        if (!$this->hasMore) {

  175.            return $this->getDisabledTextWrapper($text);

  176.        }

  177.        $url = $this->url($this->currentPage() + 1);

  178.        return $this->getPageLinkWrapper($url, $text);

  179.    }

没有更多内容。

用户评价(0)

好评度100%
  • 还没有人评论此条信息!
+ 加载更多

联系方式

提示:联系我时,请说明在巅云php学苑看到的,谢谢!
  • 联系人:
  • 地  区:
  • 电  话: 共发布信息(2004)条 所在地:未填写
看了又看
加载中
首页 首页 收藏 收藏

电话联系