当前位置:首页 > CMS教程 > Thinkphp > 列表

TP5框架实现自定义分页样式的方法示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-25 10:40:59 浏览: 评论:0 

本文实例讲述了TP5框架实现自定义分页样式的方法,分享给大家供大家参考,具体如下:

1. 在extend\目录下创建page目录,在page目录下创建Page.php文件,将以下代码放入文件中。

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

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

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

3. 分页样式为

Tags: TP5自定义分页样式

分享到: