Bootstrap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\paginator;
  12. use think\Paginator;
  13. class Bootstrap extends Paginator {
  14. /**
  15. * @return string
  16. */
  17. protected function getInfo() {
  18. $_start = $this->listRows() * ($this->currentPage() - 1) + 1;
  19. $_end = $this->listRows * $this->currentPage();
  20. $_c_rows = config('paginate.list_rows');
  21. $_10_active = '';
  22. $_25_active = '';
  23. $_50_active = '';
  24. $_100_active = '';
  25. $_page = $this->currentPage;
  26. $_25_page = $_page;
  27. $_50_page = $_page;
  28. $_100_page = $_page;
  29. /* 超出显示 那就显示最后一页 */
  30. if ($this->total() < ($_page - 1) * 25) {
  31. $_25_page = ceil($this->total() / 25);
  32. }
  33. if ($this->total() < ($_page - 1) * 50) {
  34. $_50_page = ceil($this->total() / 50);
  35. }
  36. if ($this->total() < ($_page - 1) * 100) {
  37. $_100_page = ceil($this->total() / 100);
  38. }
  39. switch ($this->listRows()) {
  40. case 25:
  41. $_25_active = 'class="active"';
  42. $_rows = 25;
  43. break;
  44. case 50:
  45. $_50_active = 'class="active"';
  46. $_rows = 50;
  47. break;
  48. case 100:
  49. $_100_active = 'class="active"';
  50. $_rows = 100;
  51. break;
  52. default:
  53. $_10_active = 'class="active"';
  54. $_rows = $_c_rows;
  55. }
  56. $_txt
  57. = <<<EOF
  58. <div class="pull-left">
  59. <span class="pagination-info">显示第 {$_start} 到第 {$_end} 条记录,总共 {$this->total()} 条记录</span>
  60. <span class="pagination-list">
  61. 每页显示
  62. <span class="btn-group dropup">
  63. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
  64. <span class="page-size">{$this->listRows()}</span>
  65. <span class="caret"></span>
  66. </button>
  67. <ul class="dropdown-menu" role="menu">
  68. <li role="menuitem" {$_10_active}><a href="{$this->url(
  69. $_page, $_c_rows
  70. )}">{$_c_rows}</a></li>
  71. <li role="menuitem" {$_25_active}><a href="{$this->url($_25_page, 25)}">25</a></li>
  72. <li role="menuitem" {$_50_active}><a href="{$this->url($_50_page, 50)}">50</a></li>
  73. <li role="menuitem" {$_100_active}><a href="{$this->url($_100_page, 100)}">100</a></li>
  74. </ul>
  75. </span>
  76. 条记录
  77. </span>
  78. <div class="pagination-go form-inline">
  79. 跳转到
  80. <input id="paginationGoInput" class="form-control">
  81. <a id="paginationGoBtn" href="{$this->url($this->currentPage(), $_rows)}" class="btn btn-default">GO</a>
  82. </div>
  83. </div>
  84. EOF;
  85. return $_txt;
  86. }
  87. /**
  88. * 上一页按钮
  89. *
  90. * @param string $text
  91. *
  92. * @return string
  93. */
  94. protected function getPreviousButton($text = "") {
  95. if (empty($text)) {
  96. if (empty($this->options['prev'])) {
  97. $text = "&laquo;";
  98. } else {
  99. $text = $this->options['prev'];
  100. }
  101. }
  102. if ($this->currentPage() <= 1) {
  103. return $this->getDisabledTextWrapper($text);
  104. }
  105. $url = $this->url($this->currentPage() - 1);
  106. return $this->getPageLinkWrapper($url, $text);
  107. }
  108. /**
  109. * 下一页按钮
  110. *
  111. * @param string $text
  112. *
  113. * @return string
  114. */
  115. protected function getNextButton($text = '') {
  116. if (empty($text)) {
  117. if (empty($this->options['next'])) {
  118. $text = "&raquo;";
  119. } else {
  120. $text = $this->options['next'];
  121. }
  122. }
  123. if (!$this->hasMore) {
  124. return $this->getDisabledTextWrapper($text);
  125. }
  126. $url = $this->url($this->currentPage() + 1);
  127. return $this->getPageLinkWrapper($url, $text);
  128. }
  129. /**
  130. * 上一页按钮
  131. *
  132. * @param string $text
  133. *
  134. * @return string
  135. */
  136. protected function getSimplePreviousButton($text = "") {
  137. if (empty($text)) {
  138. if (empty($this->options['prev'])) {
  139. $text = "&larr;";
  140. } else {
  141. $text = $this->options['prev'];
  142. }
  143. if (!empty($this->options['simple_prev'])) {
  144. $text = $this->options['simple_prev'];
  145. }
  146. }
  147. if ($this->currentPage() <= 1) {
  148. return '<li class="disabled previous page-item"><span class="page-link">'.$text.'</span></li>';
  149. }
  150. $url = $this->url($this->currentPage() - 1);
  151. return '<li class="previous page-item"><a class="page-link" href="'.htmlentities($url).'">'.$text.'</a></li>';
  152. }
  153. /**
  154. * 下一页按钮
  155. *
  156. * @param string $text
  157. *
  158. * @return string
  159. */
  160. protected function getSimpleNextButton($text = '') {
  161. if (empty($text)) {
  162. if (empty($this->options['next'])) {
  163. $text = "&rarr;";
  164. } else {
  165. $text = $this->options['next'];
  166. }
  167. if (!empty($this->options['simple_next'])) {
  168. $text = $this->options['simple_next'];
  169. }
  170. }
  171. if (!$this->hasMore) {
  172. return '<li class="disabled next page-item"><span class="page-link">'.$text.'</span></li>';
  173. }
  174. $url = $this->url($this->currentPage() + 1);
  175. return '<li class="next page-item"><a class="page-link" href="'.htmlentities($url).'">'.$text.'</a></li>';
  176. }
  177. /**
  178. * 页码按钮
  179. *
  180. * @return string
  181. */
  182. protected function getLinks() {
  183. if ($this->simple) {
  184. return '';
  185. }
  186. $block = [
  187. 'first' => null,
  188. 'slider' => null,
  189. 'last' => null
  190. ];
  191. $side = 2;
  192. $window = $side * 2;
  193. if ($this->lastPage < $window + 6) {
  194. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  195. } elseif ($this->currentPage <= $window) {
  196. $block['first'] = $this->getUrlRange(1, $window + 2);
  197. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  198. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  199. $block['first'] = $this->getUrlRange(1, 2);
  200. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 0), $this->lastPage);
  201. } else {
  202. $block['first'] = $this->getUrlRange(1, 2);
  203. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  204. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  205. }
  206. $html = '';
  207. if (is_array($block['first'])) {
  208. $html .= $this->getUrlLinks($block['first']);
  209. }
  210. if (is_array($block['slider'])) {
  211. if ($this->currentPage - $window > 1) {
  212. $html .= $this->getDots();
  213. }
  214. $html .= $this->getUrlLinks($block['slider']);
  215. }
  216. if (is_array($block['last'])) {
  217. if ($this->lastPage - $this->currentPage > $window) {
  218. $html .= $this->getDots();
  219. }
  220. $html .= $this->getUrlLinks($block['last']);
  221. }
  222. return $html;
  223. }
  224. /**
  225. * 渲染分页html
  226. *
  227. * @return mixed
  228. */
  229. public function render() {
  230. // if ($this->hasPages()) {
  231. $request = request();
  232. if ($this->simple || $request->isMobile()) {
  233. return sprintf(
  234. '%s %s',
  235. $this->getSimplePreviousButton(),
  236. $this->getSimpleNextButton()
  237. );
  238. } else {
  239. return sprintf(
  240. '%s <ul class="pagination pull-right">%s %s %s</ul>',
  241. $this->getInfo(),
  242. $this->getPreviousButton(),
  243. $this->getLinks(),
  244. $this->getNextButton()
  245. );
  246. }
  247. // }
  248. }
  249. /**
  250. * 生成一个可点击的按钮
  251. *
  252. * @param string $url
  253. * @param int $page
  254. *
  255. * @return string
  256. */
  257. protected function getAvailablePageWrapper($url, $page) {
  258. return '<li class="page-item"><a class="page-link" href="'.htmlentities($url).'">'.$page.'</a></li>';
  259. }
  260. /**
  261. * 生成一个禁用的按钮
  262. *
  263. * @param string $text
  264. *
  265. * @return string
  266. */
  267. protected function getDisabledTextWrapper($text) {
  268. return '<li class="page-item disabled"><span class="page-link">'.$text.'</span></li>';
  269. }
  270. /**
  271. * 生成一个激活的按钮
  272. *
  273. * @param string $text
  274. *
  275. * @return string
  276. */
  277. protected function getActivePageWrapper($text) {
  278. return '<li class="active page-item disabled" ><span class="page-link">'.$text.'</span></li>';
  279. }
  280. /**
  281. * 生成省略号按钮
  282. *
  283. * @return string
  284. */
  285. protected function getDots() {
  286. return $this->getDisabledTextWrapper('...');
  287. }
  288. /**
  289. * 批量生成页码按钮.
  290. *
  291. * @param array $urls
  292. *
  293. * @return string
  294. */
  295. protected function getUrlLinks(array $urls) {
  296. $html = '';
  297. foreach ($urls as $page => $url) {
  298. $html .= $this->getPageLinkWrapper($url, $page);
  299. }
  300. return $html;
  301. }
  302. /**
  303. * 生成普通页码按钮
  304. *
  305. * @param string $url
  306. * @param int $page
  307. *
  308. * @return string
  309. */
  310. protected function getPageLinkWrapper($url, $page) {
  311. if ($page == $this->currentPage()) {
  312. return $this->getActivePageWrapper($page);
  313. }
  314. return $this->getAvailablePageWrapper($url, $page);
  315. }
  316. }