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

thinkPHP5.1框架使用SemanticUI实现分页功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-09 09:52:16 浏览: 评论:0 

这篇文章主要介绍了thinkPHP5.1框架使用SemanticUI实现分页功能,结合实例形式分析了SemanticUI扩展插件的定义及使用分页相关操作技巧,需要的朋友可以参考下。

本文实例讲述了thinkPHP5.1框架使用SemanticUI实现分页功能,分享给大家供大家参考,具体如下:

1、config目录下新建paginate.php,下面是文件的内容

  1. <?php 
  2. //分页配置 
  3. return 
  4.   [ 
  5.     'type' => 'Semantic', 
  6.     'var_page' => 'page', 
  7.   ]; 

2、thinkphp\library\think\paginator\driver\下新建Semantic.php,下面是文件的内容

  1. <?php 
  2. /** 
  3.  * Created by alic(AlicFeng) on 17-6-15 下午9:17 from PhpStorm. 
  4.  * Email is alic@samego.com 
  5.  */ 
  6. namespace think\paginator\driver; 
  7. use think\Paginator; 
  8. class Semantic extends Paginator 
  9. { 
  10.   private static $previousButtonHtml = '<i class="icon left arrow"></i>'; 
  11.   private static $nextButtonHtml = '<i class="icon right arrow"></i>'; 
  12.   /** 
  13.    * 上一页按钮 
  14.    * @return string 
  15.    */ 
  16.   protected function getPreviousButton() { 
  17.     if ($this->currentPage() <= 1) { 
  18.       return $this->getDisabledTextWrapper(Semantic::$previousButtonHtml); 
  19.     } 
  20.     $url = $this->url( 
  21.       $this->currentPage() - 1 
  22.     ); 
  23.     return $this->getPageLinkWrapper($url, Semantic::$previousButtonHtml); 
  24.   } 
  25.   /** 
  26.    * 下一页按钮 
  27.    * @return string 
  28.    */ 
  29.   protected function getNextButton() { 
  30.     if (!$this->hasMore) { 
  31.       return $this->getDisabledTextWrapper(Semantic::$nextButtonHtml); 
  32.     } 
  33.     $url = $this->url($this->currentPage() + 1); 
  34.     return $this->getPageLinkWrapper($url, Semantic::$nextButtonHtml); 
  35.   } 
  36.   /** 
  37.    * 页码按钮 
  38.    * @return string 
  39.    */ 
  40.   protected function getLinks() { 
  41.     $block = [ 
  42.       'first' => null, 
  43.       'slider' => null, 
  44.       'last'  => null 
  45.     ]; 
  46.     $side  = 3; 
  47.     $window = $side * 2; 
  48.     if ($this->lastPage < $window + 6) { 
  49.       $block['first'] = $this->getUrlRange(1, $this->lastPage); 
  50.     } elseif ($this->currentPage <= $window) { 
  51.       $block['first'] = $this->getUrlRange(1, $window + 2); 
  52.       $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); 
  53.     } elseif ($this->currentPage > ($this->lastPage - $window)) { 
  54.       $block['first'] = $this->getUrlRange(1, 2); 
  55.       $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); 
  56.     } else { 
  57.       $block['first'] = $this->getUrlRange(1, 2); 
  58.       $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side); 
  59.       $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage); 
  60.     } 
  61.     $html = ''; 
  62.     if (is_array($block['first'])) { 
  63.       $html .= $this->getUrlLinks($block['first']); 
  64.     } 
  65.     if (is_array($block['slider'])) { 
  66.       $html .= $this->getDots(); 
  67.       $html .= $this->getUrlLinks($block['slider']); 
  68.     } 
  69.     if (is_array($block['last'])) { 
  70.       $html .= $this->getDots(); 
  71.       $html .= $this->getUrlLinks($block['last']); 
  72.     } 
  73.     return $html; 
  74.   } 
  75.   /** 
  76.    * 渲染分页html 
  77.    * @return mixed 
  78.    */ 
  79.   public function render() { 
  80.     if ($this->hasPages()) { 
  81.       if ($this->simple){ 
  82.         return sprintf( 
  83.           '<div style="text-align: center"><div class="ui pagination menu">%s %s</div></div>', 
  84.           $this->getPreviousButton(), 
  85.           $this->getNextButton() 
  86.         ); 
  87.       }else{ 
  88.         return sprintf( 
  89.           '<div style="text-align: center"><div class="ui pagination menu">%s %s %s</div></div>', 
  90.           $this->getPreviousButton(), 
  91.           $this->getLinks(), 
  92.           $this->getNextButton() 
  93.         ); 
  94.       } 
  95.     } 
  96.     return null; 
  97.   } 
  98.   /** 
  99.    * 生成一个可点击的按钮 
  100.    * 
  101.    * @param string $url 
  102.    * @param int $page 
  103.    * @return string 
  104.    */ 
  105.   protected function getAvailablePageWrapper($url, $page) { 
  106.     return '<a href="' . htmlentities($url) . '" rel="external nofollow" class="item">' . $page . '</a>'; 
  107.   } 
  108.   /** 
  109.    * 生成一个禁用的按钮 
  110.    * 
  111.    * @param string $text 
  112.    * @return string 
  113.    */ 
  114.   protected function getDisabledTextWrapper($text) { 
  115.     return '<a class="disabled item">' . $text . '</a>'; 
  116.   } 
  117.   /** 
  118.    * 生成一个激活的按钮 
  119.    * 
  120.    * @param string $text 
  121.    * @return string 
  122.    */ 
  123.   protected function getActivePageWrapper($text) { 
  124.     return '<a class="active item">' . $text . '</a>'; 
  125.   } 
  126.   /** 
  127.    * 生成省略号按钮 
  128.    * 
  129.    * @return string 
  130.    */ 
  131.   protected function getDots() { 
  132.     return $this->getDisabledTextWrapper('...'); 
  133.   } 
  134.   /** 
  135.    * 批量生成页码按钮. 
  136.    * 
  137.    * @param array $urls 
  138.    * @return string 
  139.    */ 
  140.   protected function getUrlLinks(array $urls) { 
  141.     $html = ''; 
  142.     foreach ($urls as $page => $url) { 
  143.       $html .= $this->getPageLinkWrapper($url, $page); 
  144.     } 
  145.     return $html; 
  146.   } 
  147.   /** 
  148.    * 生成普通页码按钮 
  149.    * 
  150.    * @param string $url 
  151.    * @param int $page 
  152.    * @return string 
  153.    */ 
  154.   protected function getPageLinkWrapper($url, $page) { 
  155.     if ($page == $this->currentPage()) { 
  156.       return $this->getActivePageWrapper($page); 
  157.     } 
  158.     return $this->getAvailablePageWrapper($url, $page); 
  159.   } 
  160. } 

3、搞定

Tags: thinkPHP5 1 SemanticUI

分享到: