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

thinkphp5+layui实现的分页样式示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-27 15:12:46 浏览: 评论:0 

这篇文章主要介绍了thinkphp5+layui实现的分页样式,结合实例形式详细分析了thinkPHP5框架结合layui实现的分页功能相关的配置、查询等操作技巧,需要的朋友可以参考下。

本文实例讲述了thinkphp5+layui实现的分页样式,分享给大家供大家参考,具体如下:

tp5之layui分页样式

1.分页类

路径:\thinkphp\library\think\paginator\driver

Layui.php

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

2.配置文件

paginate.php

  1. <?php 
  2. /** 
  3.  * @auther: xxf 
  4.  * Date: 2019/9/2 
  5.  * Time: 10:24 
  6.  */ 
  7. //分页配置 
  8. return [ 
  9.   'type' => 'Layui', 
  10.   'var_page' => 'page', 
  11. ]; 

3.模型查询

  1. public function getUserShowList($size = 20, $where = null) 
  2. { 
  3.     $res = $this 
  4.       ->field('id,title,list_order,is_top,create_time,create_time time') 
  5.       ->where($where) 
  6.       ->order(['is_top' => 'desc', 'list_order' => 'desc', 'id' => 'desc']) 
  7.       ->paginate($size); 
  8.     return $res; 
  9. } 

4.模板渲染

<div class="layui-box layui-laypage layui-laypage-molv">{$list|raw}</div>

效果

thinkphp5分页 layui分页

Tags: thinkphp5分页 layui分页

分享到: