Bootstrap.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: zhangyajun <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\paginator;
  12. use think\Paginator;
  13. class Bootstrap extends Paginator
  14. {
  15. /**
  16. * <按钮
  17. * @param string $text
  18. * @return string
  19. */
  20. protected function getPreviousButton($text = "«")
  21. {
  22. if ($this->currentPage() <= 1) {
  23. return $this->getDisabledTextWrapper($text);
  24. }
  25. $url = $this->url(
  26. $this->currentPage() - 1
  27. );
  28. return $this->getPageLinkWrapper($url, $text);
  29. }
  30. //总数标签
  31. protected function totalshow()
  32. {
  33. $totalhtml="<li class=\"disabled\"><span>共".$this->total."条记录&nbsp&nbsp第".$this->currentPage()."页/共".$this->lastPage()."页</span></li>";
  34. return $totalhtml;
  35. }
  36. //尾页标签
  37. protected function showlastpage($text = '尾页')
  38. {
  39. if($this->currentPage()==$this->lastPage())
  40. {
  41. return $this->getDisabledTextWrapper($text);
  42. }
  43. $url = $this->url($this->lastPage());
  44. return $this->getPageLinkWrapper($url, $text);
  45. }
  46. //首页标签
  47. protected function showfirstpage($text = '首页')
  48. {
  49. if($this->currentPage()==1)
  50. {
  51. return $this->getDisabledTextWrapper($text);
  52. }
  53. $url = $this->url(1);
  54. return $this->getPageLinkWrapper($url, $text);
  55. }
  56. /**
  57. * >按钮
  58. * @param string $text
  59. * @return string
  60. */
  61. protected function getNextButton($text = '»')
  62. {
  63. if (!$this->hasMore) {
  64. return $this->getDisabledTextWrapper($text);
  65. }
  66. $url = $this->url($this->currentPage() + 1);
  67. return $this->getPageLinkWrapper($url, $text);
  68. }
  69. //跳转到哪页
  70. protected function gopage()
  71. {
  72. $url = $_SERVER['REQUEST_URI'];
  73. return $gotohtml="<li><div style='display:flex;margin-left:2px;padding:0'><span style='color: #777;'>到</span> <input style='height:36px;padding:0;border:1px solid #e6e6e6; display: inline-block; width: 40px; text-align: center; margin:0px 2px;color: #777;' type='text' name='page' value='1' class='form-control'> <span style='color: #777;'>页</span> <a href=".$url." class='btn' id='pagination_gourl' style='height:20px;border-radius:0;' >确定</a></div></li>";
  74. }
  75. /**
  76. * 页码按钮
  77. * @return string
  78. */
  79. protected function getLinks()
  80. {
  81. if ($this->simple)
  82. return '';
  83. $block = [
  84. 'first' => null,
  85. 'slider' => null,
  86. 'last' => null
  87. ];
  88. $side = 2;
  89. $window = $side * 2;
  90. if ($this->lastPage < $window +1) {
  91. $block['slider'] = $this->getUrlRange(1, $this->lastPage);
  92. } elseif ($this->currentPage <= $window-1) {
  93. $block['slider'] = $this->getUrlRange(1, $window + 1);
  94. } elseif ($this->currentPage > ($this->lastPage - $window+1)) {
  95. $block['slider'] = $this->getUrlRange($this->lastPage - ($window), $this->lastPage);
  96. } else {
  97. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  98. }
  99. $html = '';
  100. if (is_array($block['first'])) {
  101. $html .= $this->getUrlLinks($block['first']);
  102. }
  103. if (is_array($block['slider'])) {
  104. $html .= $this->getUrlLinks($block['slider']);
  105. }
  106. if (is_array($block['last'])) {
  107. $html .= $this->getUrlLinks($block['last']);
  108. }
  109. return $html;
  110. }
  111. /**
  112. * 渲染分页html
  113. * @return mixed
  114. */
  115. public function render()
  116. {
  117. if ($this->hasPages()) {
  118. if ($this->simple) {
  119. return sprintf(
  120. '<ul class="pager">%s %s %s</ul>',
  121. $this->getPreviousButton(),
  122. $this->getNextButton()
  123. );
  124. } else {
  125. return sprintf(
  126. '<ul class="pagination"> %s %s %s %s %s %s %s </ul>',
  127. //显示数量页码信息
  128. $this->totalshow(),
  129. //第一页
  130. $this->showfirstpage(),
  131. //<
  132. $this->getPreviousButton(),
  133. //页码
  134. $this->getLinks(),
  135. //>
  136. $this->getNextButton(),
  137. //最后一页
  138. $this->showlastpage(),
  139. //最后再加个参数 %s 可以显示跳转到哪页
  140. $this->gopage()
  141. );
  142. }
  143. }
  144. }
  145. /**
  146. * 生成一个可点击的按钮
  147. *
  148. * @param string $url
  149. * @param int $page
  150. * @return string
  151. */
  152. protected function getAvailablePageWrapper($url, $page)
  153. {
  154. return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
  155. }
  156. /**
  157. * 生成一个禁用的按钮
  158. *
  159. * @param string $text
  160. * @return string
  161. */
  162. protected function getDisabledTextWrapper($text)
  163. {
  164. return '<li class="disabled"><span>' . $text . '</span></li>';
  165. }
  166. /**
  167. * 生成一个激活的按钮
  168. *
  169. * @param string $text
  170. * @return string
  171. */
  172. protected function getActivePageWrapper($text)
  173. {
  174. return '<li class="active"><span>' . $text . '</span></li>';
  175. }
  176. /**
  177. * 生成省略号按钮
  178. *
  179. * @return string
  180. */
  181. protected function getDots($text = '...')
  182. {
  183. //$url = $this->url($this->currentPage() + 1);
  184. // return $this->getPageLinkWrapper($url, $text);
  185. return $this->getDisabledTextWrapper('...');
  186. }
  187. /**
  188. * 批量生成页码按钮.
  189. *
  190. * @param array $urls
  191. * @return string
  192. */
  193. protected function getUrlLinks(array $urls)
  194. {
  195. $html = '';
  196. foreach ($urls as $page => $url) {
  197. $html .= $this->getPageLinkWrapper($url, $page);
  198. }
  199. return $html;
  200. }
  201. /**
  202. * 生成普通页码按钮
  203. *
  204. * @param string $url
  205. * @param int $page
  206. * @return string
  207. */
  208. protected function getPageLinkWrapper($url, $page)
  209. {
  210. if ($page == $this->currentPage()) {
  211. return $this->getActivePageWrapper($page);
  212. }
  213. return $this->getAvailablePageWrapper($url, $page);
  214. }
  215. }