Page.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // | 优化版分页插件
  11. // +----------------------------------------------------------------------
  12. // | Author: 美奇软件开发工作室 QQ:15577969
  13. // +----------------------------------------------------------------------
  14. use think\Paginator;
  15. class Page extends Paginator
  16. {
  17. //首页
  18. protected function home() {
  19. if ($this->currentPage() > 1) {
  20. return "<li class='page-item'><a class='page-link' href='" . $this->url(1) . "'>首页</a></li>";
  21. } else {
  22. return "<li class='page-item disabled'><a class='page-link'>首页</a></li>";
  23. }
  24. }
  25. //上一页
  26. protected function prev() {
  27. if ($this->currentPage() > 1) {
  28. return "<li class='page-item'><a class='page-link' href='" . $this->url($this->currentPage - 1) . "'>上一页</a></li>";
  29. } else {
  30. return "<li class='page-item disabled'><a class='page-link'>上一页</a></li>";
  31. }
  32. }
  33. //下一页
  34. protected function next() {
  35. if ($this->hasMore) {
  36. return "<li class='page-item'><a class='page-link' href='" . $this->url($this->currentPage + 1) . "'>下一页</a></li>";
  37. } else {
  38. return "<li class='page-item disabled'><a class='page-link'>下一页</a></li>";
  39. }
  40. }
  41. //尾页
  42. protected function last() {
  43. if ($this->hasMore) {
  44. return "<li class='page-item'><a class='page-link' href='" . $this->url($this->lastPage) . "'>尾页</a></li>";
  45. } else {
  46. return "<li class='page-item disabled'><a class='page-link'>尾页</a></li>";
  47. }
  48. }
  49. //统计信息
  50. protected function info(){
  51. $html='<p class="pageRemark">总共有<b>' . $this->lastPage .'</b>页,';
  52. $html.='<b>'. $this->total . '</b>条数据&nbsp;&nbsp;';
  53. $html.='<select id="pagesize"> ';
  54. $html.='<option value="5" selected="">5 条/页</option> ';
  55. $html.='<option value="10">10 条/页</option> ';
  56. $html.='<option value="20">20 条/页</option> ';
  57. $html.='<option value="30">30 条/页</option> ';
  58. $html.='<option value="50">50 条/页</option> ';
  59. $html.='</select> ';
  60. $html.='&nbsp;&nbsp;到第<input id="pageInput" type="text" min="1" value="1" class="page-input">页 ';
  61. $html.='<button type="button" href="javascript:void(0)" onclick="gotoPage()" class="page-btn">确定</button> ';
  62. $html.='</p>';
  63. return $html;
  64. }
  65. /**
  66. * 页码按钮
  67. * @return string
  68. */
  69. protected function getLinks()
  70. {
  71. $block = [
  72. 'first' => null,
  73. 'slider' => null,
  74. 'last' => null
  75. ];
  76. $side = 3;
  77. $window = $side * 2;
  78. if ($this->lastPage < $window + 6) {
  79. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  80. } elseif ($this->currentPage <= $window) {
  81. $block['first'] = $this->getUrlRange(1, $window + 2);
  82. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  83. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  84. $block['first'] = $this->getUrlRange(1, 2);
  85. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
  86. } else {
  87. $block['first'] = $this->getUrlRange(1, 2);
  88. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  89. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  90. }
  91. $html = '';
  92. if (is_array($block['first'])) {
  93. $html .= $this->getUrlLinks($block['first']);
  94. }
  95. if (is_array($block['slider'])) {
  96. $html .= $this->getDots();
  97. $html .= $this->getUrlLinks($block['slider']);
  98. }
  99. if (is_array($block['last'])) {
  100. $html .= $this->getDots();
  101. $html .= $this->getUrlLinks($block['last']);
  102. }
  103. return $html;
  104. }
  105. /**
  106. * 渲染分页html
  107. * @return mixed
  108. */
  109. public function render()
  110. {
  111. if ($this->hasPages()) {
  112. if ($this->simple) {
  113. return sprintf(
  114. '<ul class="pagination">%s %s %s</ul>',
  115. $this->prev(),
  116. $this->getLinks(),
  117. $this->next()
  118. );
  119. } else {
  120. return sprintf(
  121. '<ul class="pagination">%s %s %s %s %s</ul>',
  122. $this->home(),
  123. $this->prev(),
  124. $this->getLinks(),
  125. $this->next(),
  126. $this->last()
  127. );
  128. }
  129. }
  130. }
  131. /**
  132. * 生成一个可点击的按钮
  133. *
  134. * @param string $url
  135. * @param int $page
  136. * @return string
  137. */
  138. protected function getAvailablePageWrapper($url, $page)
  139. {
  140. return "<li class='page-item'><a class='page-link' href='" . htmlentities($url) . "'>{$page}</a></li>";
  141. }
  142. /**
  143. * 生成一个禁用的按钮
  144. *
  145. * @param string $text
  146. * @return string
  147. */
  148. protected function getDisabledTextWrapper($text)
  149. {
  150. return "<li class='page-item disabled'><a class='page-link'>{$text}</a></li>";
  151. }
  152. /**
  153. * 生成一个激活的按钮
  154. *
  155. * @param string $text
  156. * @return string
  157. */
  158. protected function getActivePageWrapper($text)
  159. {
  160. return '<li class="page-item active" aria-current="page"><a class="page-link" href="#">'.$text.'</a></li>';
  161. }
  162. /**
  163. * 生成省略号按钮
  164. *
  165. * @return string
  166. */
  167. protected function getDots()
  168. {
  169. return $this->getDisabledTextWrapper('...');
  170. }
  171. /**
  172. * 批量生成页码按钮.
  173. *
  174. * @param array $urls
  175. * @return string
  176. */
  177. protected function getUrlLinks(array $urls)
  178. {
  179. $html = '';
  180. foreach ($urls as $page => $url) {
  181. $html .= $this->getPageLinkWrapper($url, $page);
  182. }
  183. return $html;
  184. }
  185. /**
  186. * 生成普通页码按钮
  187. *
  188. * @param string $url
  189. * @param int $page
  190. * @return string
  191. */
  192. protected function getPageLinkWrapper($url, $page)
  193. {
  194. if ($page == $this->currentPage()) {
  195. return $this->getActivePageWrapper($page);
  196. }
  197. return $this->getAvailablePageWrapper($url, $page);
  198. }
  199. }