功能强大 步奏简单的分页

自定义分页类 放到extend\page,这里也可以自己决定,命名空间对了就行

    1. <?php
    2. namespace page;
    3. // +----------------------------------------------------------------------
    4. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    5. // +----------------------------------------------------------------------
    6. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
    7. // +----------------------------------------------------------------------
    8. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    9. // +----------------------------------------------------------------------
    10. // | Author: zhangyajun <[email protected]>
    11. // +----------------------------------------------------------------------

    12. use think\Paginator;

    13. class Page extends Paginator
    14. {

    15.     //首页
    16.     protected function home() {
    17.         if ($this->currentPage() > 1) {
    18.             return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
    19.         } else {
    20.             return "<p>首页</p>";
    21.         }
    22.     }

    23.     //上一页
    24.     protected function prev() {
    25.         if ($this->currentPage() > 1) {
    26.             return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
    27.         } else {
    28.             return "<p>上一页</p>";
    29.         }
    30.     }

    31.     //下一页
    32.     protected function next() {
    33.         if ($this->hasMore) {
    34.             return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
    35.         } else {
    36.             return"<p>下一页</p>";
    37.         }
    38.     }

    39.     //尾页
    40.     protected function last() {
    41.         if ($this->hasMore) {
    42.             return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
    43.         } else {
    44.             return "<p>尾页</p>";
    45.         }
    46.     }

    47.     //统计信息
    48.     protected function info(){
    49.         return "<p class='pageRemark'>共<b>" . $this->lastPage .
    50.             "</b>页<b>" . $this->total . "</b>条数据</p>";
    51.     }

    52.     /**
    53.      * 页码按钮
    54.      * @return string
    55.      */
    56.     protected function getLinks()
    57.     {

    58.         $block = [
    59.             'first'  => null,
    60.             'slider' => null,
    61.             'last'   => null
    62.         ];

    63.         $side   = 3;
    64.         $window = $side * 2;

    65.         if ($this->lastPage < $window + 6) {
    66.             $block['first'] = $this->getUrlRange(1, $this->lastPage);
    67.         } elseif ($this->currentPage <= $window) {
    68.             $block['first'] = $this->getUrlRange(1, $window + 2);
    69.             $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
    70.         } elseif ($this->currentPage > ($this->lastPage - $window)) {
    71.             $block['first'] = $this->getUrlRange(1, 2);
    72.             $block['last']  = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
    73.         } else {
    74.             $block['first']  = $this->getUrlRange(1, 2);
    75.             $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
    76.             $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
    77.         }

    78.         $html = '';

    79.         if (is_array($block['first'])) {
    80.             $html .= $this->getUrlLinks($block['first']);
    81.         }

    82.         if (is_array($block['slider'])) {
    83.             $html .= $this->getDots();
    84.             $html .= $this->getUrlLinks($block['slider']);
    85.         }

    86.         if (is_array($block['last'])) {
    87.             $html .= $this->getDots();
    88.             $html .= $this->getUrlLinks($block['last']);
    89.         }

    90.         return $html;
    91.     }

    92.     /**
    93.      * 渲染分页html
    94.      * @return mixed
    95.      */
    96.     public function render()
    97.     {
    98.         if ($this->hasPages()) {
    99.             if ($this->simple) {
    100.                 return sprintf(
    101.                     '%s<div class="pagination">%s %s %s</div>',
    102.                     $this->css(),
    103.                     $this->prev(),
    104.                     $this->getLinks(),
    105.                     $this->next()
    106.                 );
    107.             } else {
    108.                 return sprintf(
    109.                     '%s<div class="pagination">%s %s %s %s %s %s</div>',
    110.                     $this->css(),
    111.                     $this->home(),
    112.                     $this->prev(),
    113.                     $this->getLinks(),
    114.                     $this->next(),
    115.                     $this->last(),
    116.                     $this->info()
    117.                 );
    118.             }
    119.         }
    120.     }

    121.     /**
    122.      * 生成一个可点击的按钮
    123.      *
    124.      * @param  string $url
    125.      * @param  int    $page
    126.      * @return string
    127.      */
    128.     protected function getAvailablePageWrapper($url, $page)
    129.     {
    130.         return '<a href="' . htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . '</a>';
    131.     }

    132.     /**
    133.      * 生成一个禁用的按钮
    134.      *
    135.      * @param  string $text
    136.      * @return string
    137.      */
    138.     protected function getDisabledTextWrapper($text)
    139.     {
    140.         return '<p class="pageEllipsis">' . $text . '</p>';
    141.     }

    142.     /**
    143.      * 生成一个激活的按钮
    144.      *
    145.      * @param  string $text
    146.      * @return string
    147.      */
    148.     protected function getActivePageWrapper($text)
    149.     {
    150.         return '<a href="" class="cur">' . $text . '</a>';
    151.     }

    152.     /**
    153.      * 生成省略号按钮
    154.      *
    155.      * @return string
    156.      */
    157.     protected function getDots()
    158.     {
    159.         return $this->getDisabledTextWrapper('...');
    160.     }

    161.     /**
    162.      * 批量生成页码按钮.
    163.      *
    164.      * @param  array $urls
    165.      * @return string
    166.      */
    167.     protected function getUrlLinks(array $urls)
    168.     {
    169.         $html = '';

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

    173.         return $html;
    174.     }

    175.     /**
    176.      * 生成普通页码按钮
    177.      *
    178.      * @param  string $url
    179.      * @param  int    $page
    180.      * @return string
    181.      */
    182.     protected function getPageLinkWrapper($url, $page)
    183.     {
    184.         if ($page == $this->currentPage()) {
    185.             return $this->getActivePageWrapper($page);
    186.         }

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

    189.     /**
    190.      * 分页样式
    191.      */
    192.     protected function css(){
    193.         return '  <style type="text/css">
    194.             .pagination p{
    195.                 margin:0;
    196.                 cursor:pointer
    197.             }
    198.             .pagination{
    199.                 height:40px;
    200.                 padding:20px 0px;
    201.             }
    202.             .pagination a{
    203.                 display:block;
    204.                 float:left;
    205.                 margin-right:10px;
    206.                 padding:2px 12px;
    207.                 height:24px;
    208.                 border:1px #cccccc solid;
    209.                 background:#fff;
    210.                 text-decoration:none;
    211.                 color:#808080;
    212.                 font-size:12px;
    213.                 line-height:24px;
    214.             }
    215.             .pagination a:hover{
    216.                 color:#077ee3;
    217.                 background: white;
    218.                 border:1px #077ee3 solid;
    219.             }
    220.             .pagination a.cur{
    221.                 border:none;
    222.                 background:#077ee3;
    223.                 color:#fff;
    224.             }
    225.             .pagination p{
    226.                 float:left;
    227.                 padding:2px 12px;
    228.                 font-size:12px;
    229.                 height:24px;
    230.                 line-height:24px;
    231.                 color:#bbb;
    232.                 border:1px #ccc solid;
    233.                 background:#fcfcfc;
    234.                 margin-right:8px;

    235.             }
    236.             .pagination p.pageRemark{
    237.                 border-style:none;
    238.                 background:none;
    239.                 margin-right:0px;
    240.                 padding:4px 0px;
    241.                 color:#666;
    242.             }
    243.             .pagination p.pageRemark b{
    244.                 color:red;
    245.             }
    246.             .pagination p.pageEllipsis{
    247.                 border-style:none;
    248.                 background:none;
    249.                 padding:4px 0px;
    250.                 color:#808080;
    251.             }
    252.             .dates li {font-size: 14px;margin:20px 0}
    253.             .dates li span{float:right}
    254.         </style>';
    255.     }
    256. }

  修改配置文件

  • //分页配置
  •     'paginate'               => [
  •         'type'      => 'page\Page',
  •         'var_page'  => 'page',
  •         'list_rows' => 15,
  •     ],

  视图里面
  

<center>{$data->render()}</center>

  控制器

  $data = Db::table("admin")->field('id,name,password,status,type')->paginate(2);

  

猜你喜欢

转载自www.cnblogs.com/zghao/p/8856451.html