164 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			164 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								namespace tool;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use think\Paginator;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * 自定义分页模板
							 | 
						||
| 
								 | 
							
								 * Class CustomPageHelper
							 | 
						||
| 
								 | 
							
								 * @package tool
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class CustomPageHelper extends Paginator
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    // 中间页码按钮个数
							 | 
						||
| 
								 | 
							
								    protected $btnNumber = 10;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 首页
							 | 
						||
| 
								 | 
							
								    protected function getFirstButton($str = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($this->currentPage() <= 1) {
							 | 
						||
| 
								 | 
							
								            return $this->getDisabledTextWrapper($str);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $url = $this->url(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $this->getPageLinkWrapper($url, $str);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 上一页
							 | 
						||
| 
								 | 
							
								    protected function getPreviousButton($text = "«")
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($this->currentPage() <= 1) {
							 | 
						||
| 
								 | 
							
								            return $this->getDisabledTextWrapper($text, 'prev');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $url = $this->url(
							 | 
						||
| 
								 | 
							
								            $this->currentPage() - 1
							 | 
						||
| 
								 | 
							
								        );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $this->getPageLinkWrapper($url, $text, 'prev');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 页码
							 | 
						||
| 
								 | 
							
								    protected function getLinks()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($this->total > $this->listRows) {
							 | 
						||
| 
								 | 
							
								            if ($this->lastPage < $this->btnNumber) {
							 | 
						||
| 
								 | 
							
								                return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage), false);
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                $min = 1;
							 | 
						||
| 
								 | 
							
								                if ($this->currentPage > $this->btnNumber / 2) $min = $this->currentPage - floor($this->btnNumber / 2);
							 | 
						||
| 
								 | 
							
								                if ($this->lastPage - $this->currentPage < $this->btnNumber / 2) $min = $this->lastPage - $this->btnNumber + 1;
							 | 
						||
| 
								 | 
							
								                $max = $min + $this->btnNumber - 1;
							 | 
						||
| 
								 | 
							
								                $ellipsis = false;
							 | 
						||
| 
								 | 
							
								                if ($this->lastPage > $max)  {
							 | 
						||
| 
								 | 
							
								                    $ellipsis = true;
							 | 
						||
| 
								 | 
							
								                    $max = $max - 1;
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                return $this->getUrlLinks($this->getUrlRange($min, $max), $ellipsis);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 下一页
							 | 
						||
| 
								 | 
							
								    protected function getNextButton($text = '»')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$this->hasMore) {
							 | 
						||
| 
								 | 
							
								            return $this->getDisabledTextWrapper($text, 'next');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $url = $this->url($this->currentPage() + 1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $this->getPageLinkWrapper($url, $text, 'next');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 末页
							 | 
						||
| 
								 | 
							
								    protected function getLastButton($text = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$this->hasMore) {
							 | 
						||
| 
								 | 
							
								            return $this->getDisabledTextWrapper($text);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $url = $this->url($this->lastPage());
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $this->getPageLinkWrapper($url, $text);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 渲染分页
							 | 
						||
| 
								 | 
							
								    public function render($type = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        //数据是否足够分页
							 | 
						||
| 
								 | 
							
								        if ($this->hasPages()) {
							 | 
						||
| 
								 | 
							
								            if(strtoupper($type) == 'A') {
							 | 
						||
| 
								 | 
							
								                return sprintf(
							 | 
						||
| 
								 | 
							
								                    '<ul class="pageing">%s %s %s %s</ul>',
							 | 
						||
| 
								 | 
							
								                    $this->getFirstButton('首页'),
							 | 
						||
| 
								 | 
							
								                    $this->getPreviousButton('上一页'),
							 | 
						||
| 
								 | 
							
								                    $this->getLinks(),
							 | 
						||
| 
								 | 
							
								                    $this->getNextButton('下一页')
							 | 
						||
| 
								 | 
							
								                );
							 | 
						||
| 
								 | 
							
								            } elseif (strtoupper($type) == 'B') {
							 | 
						||
| 
								 | 
							
								                return sprintf(
							 | 
						||
| 
								 | 
							
								                    '<ul class="pageing">%s %s %s %s %s</ul>',
							 | 
						||
| 
								 | 
							
								                    $this->getFirstButton('首页'),
							 | 
						||
| 
								 | 
							
								                    $this->getPreviousButton('上一页'),
							 | 
						||
| 
								 | 
							
								                    $this->getLinks(),
							 | 
						||
| 
								 | 
							
								                    $this->getNextButton('下一页'),
							 | 
						||
| 
								 | 
							
								                    $this->getLastButton('末页')
							 | 
						||
| 
								 | 
							
								                );
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                return sprintf(
							 | 
						||
| 
								 | 
							
								                    '<ul class="pageing">%s %s %s</ul>',
							 | 
						||
| 
								 | 
							
								                    $this->getPreviousButton('上一页'),
							 | 
						||
| 
								 | 
							
								                    $this->getLinks(),
							 | 
						||
| 
								 | 
							
								                    $this->getNextButton('下一页')
							 | 
						||
| 
								 | 
							
								                );
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 生成禁用按钮
							 | 
						||
| 
								 | 
							
								    protected function getDisabledTextWrapper($text, $htmlClass = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return '<li class="disabled '.$htmlClass.'"><span>' . $text . '</span></li>';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 生成普通按钮
							 | 
						||
| 
								 | 
							
								    protected function getPageLinkWrapper($url, $page, $htmlClass = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($page == $this->currentPage()) {
							 | 
						||
| 
								 | 
							
								            return $this->getActivePageWrapper($page, $htmlClass);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $this->getAvailablePageWrapper($url, $page, $htmlClass);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 生成当前页按钮
							 | 
						||
| 
								 | 
							
								    protected function getActivePageWrapper($text, $htmlClass = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return '<li class="cur "'.$htmlClass.'><span>' . $text . '</span></li>';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 可点击按钮
							 | 
						||
| 
								 | 
							
								    protected function getAvailablePageWrapper($url, $page, $htmlClass = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return '<li class="'.$htmlClass.'"><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 批量生成页码按钮
							 | 
						||
| 
								 | 
							
								    protected function getUrlLinks(array $urls, bool $ellipsis, $htmlClass = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $html = '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        foreach ($urls as $page => $url) {
							 | 
						||
| 
								 | 
							
								            $html .= $this->getPageLinkWrapper($url, $page, $htmlClass);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if($ellipsis) {
							 | 
						||
| 
								 | 
							
								            $html .= $this->getDisabledTextWrapper('...', $htmlClass);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return $html;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |