{{title}}
生活图文
首页 Article 返回

Thinkphp5分页模板如何兼容bootstrap4

发布日期:2020-09-04 11:03:40

thinkphp5分页模板在引入bootstrap4后,分页样式无效果

1、首选确认bootstrap是否引入成功

2、比较bootstrap3和4分页的区别

bootstrap3使用的class类

bootstrap4使用的class类

比较发现bootstrap4分页中

li标签比之前添加了page-item类;a标签添加了page-link类,另外还新增了pagination-lg,pagination-sm来控制大小,所以致使页面分页样式不生效,所以需要修改thinkphp5分页生成文件


项目>thinkphp>library>think>paginator>driver>Bootstrap.php

修改兼容bootstrap3和4


<?php

namespace think\paginator\driver;

use think\Paginator;

class Bootstrap extends Paginator
{


    protected function getPreviousButton($text = "&laquo;")
    {

        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url(
            $this->currentPage() - 1
        );

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

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    protected function getNextButton($text = '&raquo;')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

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

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


    protected function getLinks()
    {
        if ($this->simple)
            return '';

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

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

        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;
    }


    public function render()
    {
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '<ul class="pager justify-content-center text-justify row">%s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '<ul class="pagination pagination-lg justify-content-center text-justify row">%s %s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }


    protected function getAvailablePageWrapper($url, $page)
    {
        return '<li class="page-item"><a class="page-link"  href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }


    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled page-item"><span class="page-link" >' . $text . '</span></li>';
    }


    protected function getActivePageWrapper($text)
    {
        return '<li class="active page-item"><span  class="page-link">' . $text . '</span></li>';
    }


    protected function getDots()
    {
        return $this->getDisabledTextWrapper('...');
    }


    protected function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }


    protected function getPageLinkWrapper($url, $page)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }

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



微信 支付宝 微信 支付宝
Copyright © 2022 All rights reserved .Meng
生活图文