当前位置:Gxlcms > PHP教程 > 最详细的ThinkPHP5自定义分页类教程

最详细的ThinkPHP5自定义分页类教程

时间:2021-07-01 10:21:17 帮助过:91人阅读

作为一名PHP程序员在编写列表性质的页面时,几乎都要写上一段分页的程序。分页的方法有很多种,本文我们就给大家详细介绍一下ThinkPHP5自定义分页类教程。

第一步:创建文件Page.php,将其放到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 <448901948@qq.com>
  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. }

第二步:修改配置文件

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

效果图预览:

1511831364433759.png

以上就是thinkPHP5.0自定义分页类教程详解,大家可以动手自己做一个, 希望能帮助到大家。

相关推荐:

laravel自定义分页是如何实现的?

laravel框架中关于自定义分页效果的实例分析

PHP 自定义分页函数_PHP教程

以上就是最详细的ThinkPHP5自定义分页类教程的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行