Bootstrap.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 think\paginator\driver;
  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 = "&laquo;")
  21. {
  22. if ($this->currentPage() <= 1) {
  23. return $this->getDisabledTextWrapper($text);
  24. }
  25. $url = $this->url(
  26. $this->currentPage() - 1
  27. );
  28. $url = "http://www.tp5.com".$url;
  29. return $this->getPageLinkWrapper($url, $text);
  30. }
  31. /**
  32. * 下一页按钮
  33. * @param string $text
  34. * @return string
  35. */
  36. protected function getNextButton($text = '&raquo;')
  37. {
  38. if (!$this->hasMore) {
  39. return $this->getDisabledTextWrapper($text);
  40. }
  41. $url = $this->url($this->currentPage() + 1);
  42. $url = "http://www.tp5.com".$url;
  43. return $this->getPageLinkWrapper($url, $text);
  44. }
  45. /**
  46. * 页码按钮
  47. * @return string
  48. */
  49. protected function getLinks()
  50. {
  51. if ($this->simple)
  52. return '';
  53. $block = [
  54. 'first' => null,
  55. 'slider' => null,
  56. 'last' => null
  57. ];
  58. $side = 3;
  59. $window = $side * 2;
  60. if ($this->lastPage < $window + 6) {
  61. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  62. } elseif ($this->currentPage <= $window) {
  63. $block['first'] = $this->getUrlRange(1, $window + 2);
  64. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  65. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  66. $block['first'] = $this->getUrlRange(1, 2);
  67. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
  68. } else {
  69. $block['first'] = $this->getUrlRange(1, 2);
  70. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  71. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  72. }
  73. $html = '';
  74. if (is_array($block['first'])) {
  75. $html .= $this->getUrlLinks($block['first']);
  76. }
  77. if (is_array($block['slider'])) {
  78. $html .= $this->getDots();
  79. $html .= $this->getUrlLinks($block['slider']);
  80. }
  81. if (is_array($block['last'])) {
  82. $html .= $this->getDots();
  83. $html .= $this->getUrlLinks($block['last']);
  84. }
  85. return $html;
  86. }
  87. /**
  88. * 渲染分页html
  89. * @return mixed
  90. */
  91. public function render()
  92. {
  93. if ($this->hasPages()) {
  94. if ($this->simple) {
  95. return sprintf(
  96. '<ul class="pager">%s %s</ul>',
  97. $this->getPreviousButton(),
  98. $this->getNextButton()
  99. );
  100. } else {
  101. return sprintf(
  102. '<ul class="pagination">%s %s %s</ul>',
  103. $this->getPreviousButton(),
  104. $this->getLinks(),
  105. $this->getNextButton()
  106. );
  107. }
  108. }
  109. }
  110. /**
  111. * 生成一个可点击的按钮
  112. *
  113. * @param string $url
  114. * @param int $page
  115. * @return string
  116. */
  117. protected function getAvailablePageWrapper($url, $page)
  118. {
  119. $jump = substr($url,-1,1);
  120. return '<li><a class="button big" onclick="show('.$jump.')">' . $page . '</a></li>';
  121. }
  122. /**
  123. * 生成一个禁用的按钮
  124. *
  125. * @param string $text
  126. * @return string
  127. */
  128. protected function getDisabledTextWrapper($text)
  129. {
  130. return '<li class="disabled button big"><span>' . $text . '</span></li>';
  131. //return '<li class="disabled"><span>' . $text . '</span></li>';
  132. }
  133. /**
  134. * 生成一个激活的按钮
  135. *
  136. * @param string $text
  137. * @return string
  138. */
  139. protected function getActivePageWrapper($text)
  140. {
  141. return '<li class="active"><span>' . $text . '</span></li>';
  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. }