Page.php 7.1 KB

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