Bootstrap.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. return $this->getPageLinkWrapper($url, $text);
  29. }
  30. /**
  31. * 下一页按钮
  32. * @param string $text
  33. * @return string
  34. */
  35. protected function getNextButton($text = '&raquo;')
  36. {
  37. if (!$this->hasMore) {
  38. return $this->getDisabledTextWrapper($text);
  39. }
  40. $url = $this->url($this->currentPage() + 1);
  41. return $this->getPageLinkWrapper($url, $text);
  42. }
  43. /**
  44. * 页码按钮
  45. * @return string
  46. */
  47. protected function getLinks()
  48. {
  49. if ($this->simple) {
  50. return '';
  51. }
  52. $block = [
  53. 'first' => null,
  54. 'slider' => null,
  55. 'last' => null,
  56. ];
  57. $side = 3;
  58. $window = $side * 2;
  59. if ($this->lastPage < $window + 6) {
  60. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  61. } elseif ($this->currentPage <= $window) {
  62. $block['first'] = $this->getUrlRange(1, $window + 2);
  63. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  64. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  65. $block['first'] = $this->getUrlRange(1, 2);
  66. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
  67. } else {
  68. $block['first'] = $this->getUrlRange(1, 2);
  69. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  70. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  71. }
  72. $html = '';
  73. if (is_array($block['first'])) {
  74. $html .= $this->getUrlLinks($block['first']);
  75. }
  76. if (is_array($block['slider'])) {
  77. $html .= $this->getDots();
  78. $html .= $this->getUrlLinks($block['slider']);
  79. }
  80. if (is_array($block['last'])) {
  81. $html .= $this->getDots();
  82. $html .= $this->getUrlLinks($block['last']);
  83. }
  84. return $html;
  85. }
  86. /**
  87. * 渲染分页html
  88. * @return mixed
  89. */
  90. public function render()
  91. {
  92. if ($this->hasPages()) {
  93. if ($this->simple) {
  94. return sprintf(
  95. '<ul class="pager">%s %s</ul>',
  96. $this->getPreviousButton(),
  97. $this->getNextButton()
  98. );
  99. } else {
  100. return sprintf(
  101. '<ul class="pagination">%s %s %s</ul>',
  102. $this->getPreviousButton(),
  103. $this->getLinks(),
  104. $this->getNextButton()
  105. );
  106. }
  107. }
  108. }
  109. /**
  110. * 生成一个可点击的按钮
  111. *
  112. * @param string $url
  113. * @param int $page
  114. * @return string
  115. */
  116. protected function getAvailablePageWrapper($url, $page)
  117. {
  118. return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
  119. }
  120. /**
  121. * 生成一个禁用的按钮
  122. *
  123. * @param string $text
  124. * @return string
  125. */
  126. protected function getDisabledTextWrapper($text)
  127. {
  128. return '<li class="disabled"><span>' . $text . '</span></li>';
  129. }
  130. /**
  131. * 生成一个激活的按钮
  132. *
  133. * @param string $text
  134. * @return string
  135. */
  136. protected function getActivePageWrapper($text)
  137. {
  138. return '<li class="active"><span>' . $text . '</span></li>';
  139. }
  140. /**
  141. * 生成省略号按钮
  142. *
  143. * @return string
  144. */
  145. protected function getDots()
  146. {
  147. return $this->getDisabledTextWrapper('...');
  148. }
  149. /**
  150. * 批量生成页码按钮.
  151. *
  152. * @param array $urls
  153. * @return string
  154. */
  155. protected function getUrlLinks(array $urls)
  156. {
  157. $html = '';
  158. foreach ($urls as $page => $url) {
  159. $html .= $this->getPageLinkWrapper($url, $page);
  160. }
  161. return $html;
  162. }
  163. /**
  164. * 生成普通页码按钮
  165. *
  166. * @param string $url
  167. * @param int $page
  168. * @return string
  169. */
  170. protected function getPageLinkWrapper($url, $page)
  171. {
  172. if ($this->currentPage() == $page) {
  173. return $this->getActivePageWrapper($page);
  174. }
  175. return $this->getAvailablePageWrapper($url, $page);
  176. }
  177. }