TP5之自定义分页样式

  1. 分页样式为
  2. 在extend\目录下创建page目录,在page目录下创建Page.php文件,将以下代码放入文件中。
  3.   1 <?php
      2 namespace page;
      3 
      4 use think\Paginator;
      5 
      6 class Page extends Paginator
      7 {
      8 
      9     //首页
     10     protected function home() {
     11         if ($this->currentPage() > 1) {
     12             return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
     13         } else {
     14             return "<p>首页</p>";
     15         }
     16     }
     17 
     18     //上一页
     19     protected function prev() {
     20         if ($this->currentPage() > 1) {
     21             return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
     22         } else {
     23             return "<p>上一页</p>";
     24         }
     25     }
     26 
     27     //下一页
     28     protected function next() {
     29         if ($this->hasMore) {
     30             return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
     31         } else {
     32             return"<p>下一页</p>";
     33         }
     34     }
     35 
     36     //尾页
     37     protected function last() {
     38         if ($this->hasMore) {
     39             return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
     40         } else {
     41             return "<p>尾页</p>";
     42         }
     43     }
     44 
     45     //统计信息
     46     protected function info(){
     47         return "<p class='pageRemark'>共<b>" . $this->lastPage .
     48             "</b>页<b>" . $this->total . "</b>条数据</p>";
     49     }
     50 
     51     /**
     52      * 页码按钮
     53      * @return string
     54      */
     55     protected function getLinks()
     56     {
     57 
     58         $block = [
     59             'first'  => null,
     60             'slider' => null,
     61             'last'   => null
     62         ];
     63 
     64         $side   = 3;
     65         $window = $side * 2;
     66 
     67         if ($this->lastPage < $window + 6) {
     68             $block['first'] = $this->getUrlRange(1, $this->lastPage);
     69         } elseif ($this->currentPage <= $window) {
     70             $block['first'] = $this->getUrlRange(1, $window + 2);
     71             $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
     72         } elseif ($this->currentPage > ($this->lastPage - $window)) {
     73             $block['first'] = $this->getUrlRange(1, 2);
     74             $block['last']  = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
     75         } else {
     76             $block['first']  = $this->getUrlRange(1, 2);
     77             $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
     78             $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
     79         }
     80 
     81         $html = '';
     82 
     83         if (is_array($block['first'])) {
     84             $html .= $this->getUrlLinks($block['first']);
     85         }
     86 
     87         if (is_array($block['slider'])) {
     88             $html .= $this->getDots();
     89             $html .= $this->getUrlLinks($block['slider']);
     90         }
     91 
     92         if (is_array($block['last'])) {
     93             $html .= $this->getDots();
     94             $html .= $this->getUrlLinks($block['last']);
     95         }
     96 
     97         return $html;
     98     }
     99 
    100     /**
    101      * 渲染分页html
    102      * @return mixed
    103      */
    104     public function render()
    105     {
    106         if ($this->hasPages()) {
    107             if ($this->simple) {
    108                 return sprintf(
    109                     '%s<div class="pagination">%s %s %s</div>',
    110                     $this->css(),
    111                     $this->prev(),
    112                     $this->getLinks(),
    113                     $this->next()
    114                 );
    115             } else {
    116                 return sprintf(
    117                     '%s<div class="pagination">%s %s %s %s %s %s</div>',
    118                     $this->css(),
    119                     $this->home(),
    120                     $this->prev(),
    121                     $this->getLinks(),
    122                     $this->next(),
    123                     $this->last(),
    124                     $this->info()
    125                 );
    126             }
    127         }
    128     }
    129 
    130     /**
    131      * 生成一个可点击的按钮
    132      *
    133      * @param  string $url
    134      * @param  int    $page
    135      * @return string
    136      */
    137     protected function getAvailablePageWrapper($url, $page)
    138     {
    139         return '<a href="' . htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . '</a>';
    140     }
    141 
    142     /**
    143      * 生成一个禁用的按钮
    144      *
    145      * @param  string $text
    146      * @return string
    147      */
    148     protected function getDisabledTextWrapper($text)
    149     {
    150         return '<p class="pageEllipsis">' . $text . '</p>';
    151     }
    152 
    153     /**
    154      * 生成一个激活的按钮
    155      *
    156      * @param  string $text
    157      * @return string
    158      */
    159     protected function getActivePageWrapper($text)
    160     {
    161         return '<a href="" class="cur">' . $text . '</a>';
    162     }
    163 
    164     /**
    165      * 生成省略号按钮
    166      *
    167      * @return string
    168      */
    169     protected function getDots()
    170     {
    171         return $this->getDisabledTextWrapper('...');
    172     }
    173 
    174     /**
    175      * 批量生成页码按钮.
    176      *
    177      * @param  array $urls
    178      * @return string
    179      */
    180     protected function getUrlLinks(array $urls)
    181     {
    182         $html = '';
    183 
    184         foreach ($urls as $page => $url) {
    185             $html .= $this->getPageLinkWrapper($url, $page);
    186         }
    187 
    188         return $html;
    189     }
    190 
    191     /**
    192      * 生成普通页码按钮
    193      *
    194      * @param  string $url
    195      * @param  int    $page
    196      * @return string
    197      */
    198     protected function getPageLinkWrapper($url, $page)
    199     {
    200         if ($page == $this->currentPage()) {
    201             return $this->getActivePageWrapper($page);
    202         }
    203 
    204         return $this->getAvailablePageWrapper($url, $page);
    205     }
    206 
    207     /**
    208      * 分页样式
    209      */
    210     protected function css(){
    211         return '  <style type="text/css">
    212             .pagination p{
    213                 margin:0;
    214                 cursor:pointer
    215             }
    216             .pagination{
    217                 height:40px;
    218                 padding:20px 0px;
    219             }
    220             .pagination a{
    221                 display:block;
    222                 float:left;
    223                 margin-right:10px;
    224                 padding:2px 12px;
    225                 height:24px;
    226                 border:1px #cccccc solid;
    227                 background:#fff;
    228                 text-decoration:none;
    229                 color:#808080;
    230                 font-size:12px;
    231                 line-height:24px;
    232             }
    233             .pagination a:hover{
    234                 color:#077ee3;
    235                 background: white;
    236                 border:1px #077ee3 solid;
    237             }
    238             .pagination a.cur{
    239                 border:none;
    240                 background:#077ee3;
    241                 color:#fff;
    242             }
    243             .pagination p{
    244                 float:left;
    245                 padding:2px 12px;
    246                 font-size:12px;
    247                 height:24px;
    248                 line-height:24px;
    249                 color:#bbb;
    250                 border:1px #ccc solid;
    251                 background:#fcfcfc;
    252                 margin-right:8px;
    253 
    254             }
    255             .pagination p.pageRemark{
    256                 border-style:none;
    257                 background:none;
    258                 margin-right:0px;
    259                 padding:4px 0px;
    260                 color:#666;
    261             }
    262             .pagination p.pageRemark b{
    263                 color:red;
    264             }
    265             .pagination p.pageEllipsis{
    266                 border-style:none;
    267                 background:none;
    268                 padding:4px 0px;
    269                 color:#808080;
    270             }
    271             .dates li {font-size: 14px;margin:20px 0}
    272             .dates li span{float:right}
    273         </style>';
    274     }
    275 }

     

    修改  application\config.php  中的配置文件即可

 

1 //分页配置  
2     'paginate'               => [  
3         'type'      => 'page\Page',//分页类  
4         'var_page'  => 'page',  
5         'list_rows' => 15,  
6     ],

 

over!over!over!

 

posted @ 2018-11-02 09:24  为牧  阅读(4282)  评论(0编辑  收藏  举报