123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace cmf\paginator;
- use think\Paginator;
- class Bootstrap extends Paginator {
-
- protected function getInfo() {
- $_start = $this->listRows() * ($this->currentPage() - 1) + 1;
- $_end = $this->listRows * $this->currentPage();
- $_c_rows = config('paginate.list_rows');
- $_10_active = '';
- $_25_active = '';
- $_50_active = '';
- $_100_active = '';
- $_page = $this->currentPage;
- $_25_page = $_page;
- $_50_page = $_page;
- $_100_page = $_page;
-
- if ($this->total() < ($_page - 1) * 25) {
- $_25_page = ceil($this->total() / 25);
- }
- if ($this->total() < ($_page - 1) * 50) {
- $_50_page = ceil($this->total() / 50);
- }
- if ($this->total() < ($_page - 1) * 100) {
- $_100_page = ceil($this->total() / 100);
- }
- switch ($this->listRows()) {
- case 25:
- $_25_active = 'class="active"';
- $_rows = 25;
- break;
- case 50:
- $_50_active = 'class="active"';
- $_rows = 50;
- break;
- case 100:
- $_100_active = 'class="active"';
- $_rows = 100;
- break;
- default:
- $_10_active = 'class="active"';
- $_rows = $_c_rows;
- }
- $_txt
- = <<<EOF
- <div class="pull-left">
- <span class="pagination-info">显示第 {$_start} 到第 {$_end} 条记录,总共 {$this->total()} 条记录</span>
- <span class="pagination-list">
- 每页显示
- <span class="btn-group dropup">
- <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
- <span class="page-size">{$this->listRows()}</span>
- <span class="caret"></span>
- </button>
- <ul class="dropdown-menu" role="menu">
- <li role="menuitem" {$_10_active}><a href="{$this->url(
- $_page, $_c_rows
- )}">{$_c_rows}</a></li>
- <li role="menuitem" {$_25_active}><a href="{$this->url($_25_page, 25)}">25</a></li>
- <li role="menuitem" {$_50_active}><a href="{$this->url($_50_page, 50)}">50</a></li>
- <li role="menuitem" {$_100_active}><a href="{$this->url($_100_page, 100)}">100</a></li>
- </ul>
- </span>
- 条记录
- </span>
- <div class="pagination-go form-inline">
- 跳转到
- <input id="paginationGoInput" class="form-control">
- <a id="paginationGoBtn" href="{$this->url($this->currentPage(), $_rows)}" class="btn btn-default">GO</a>
- </div>
- </div>
- EOF;
- return $_txt;
- }
-
- protected function getPreviousButton($text = "") {
- if (empty($text)) {
- if (empty($this->options['prev'])) {
- $text = "«";
- } else {
- $text = $this->options['prev'];
- }
- }
- if ($this->currentPage() <= 1) {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->currentPage() - 1);
- return $this->getPageLinkWrapper($url, $text);
- }
-
- protected function getNextButton($text = '') {
- if (empty($text)) {
- if (empty($this->options['next'])) {
- $text = "»";
- } else {
- $text = $this->options['next'];
- }
- }
- if (!$this->hasMore) {
- return $this->getDisabledTextWrapper($text);
- }
- $url = $this->url($this->currentPage() + 1);
- return $this->getPageLinkWrapper($url, $text);
- }
-
- protected function getSimplePreviousButton($text = "") {
- if (empty($text)) {
- if (empty($this->options['prev'])) {
- $text = "←";
- } else {
- $text = $this->options['prev'];
- }
- if (!empty($this->options['simple_prev'])) {
- $text = $this->options['simple_prev'];
- }
- }
- if ($this->currentPage() <= 1) {
- return '<li class="disabled previous page-item"><span class="page-link">'.$text.'</span></li>';
- }
- $url = $this->url($this->currentPage() - 1);
- return '<li class="previous page-item"><a class="page-link" href="'.htmlentities($url).'">'.$text.'</a></li>';
- }
-
- protected function getSimpleNextButton($text = '') {
- if (empty($text)) {
- if (empty($this->options['next'])) {
- $text = "→";
- } else {
- $text = $this->options['next'];
- }
- if (!empty($this->options['simple_next'])) {
- $text = $this->options['simple_next'];
- }
- }
- if (!$this->hasMore) {
- return '<li class="disabled next page-item"><span class="page-link">'.$text.'</span></li>';
- }
- $url = $this->url($this->currentPage() + 1);
- return '<li class="next page-item"><a class="page-link" href="'.htmlentities($url).'">'.$text.'</a></li>';
- }
-
- protected function getLinks() {
- if ($this->simple) {
- return '';
- }
- $block = [
- 'first' => null,
- 'slider' => null,
- 'last' => null
- ];
- $side = 2;
- $window = $side * 2;
- if ($this->lastPage < $window + 6) {
- $block['first'] = $this->getUrlRange(1, $this->lastPage);
- } elseif ($this->currentPage <= $window) {
- $block['first'] = $this->getUrlRange(1, $window + 2);
- $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
- } elseif ($this->currentPage > ($this->lastPage - $window)) {
- $block['first'] = $this->getUrlRange(1, 2);
- $block['last'] = $this->getUrlRange($this->lastPage - ($window + 0), $this->lastPage);
- } else {
- $block['first'] = $this->getUrlRange(1, 2);
- $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
- $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
- }
- $html = '';
- if (is_array($block['first'])) {
- $html .= $this->getUrlLinks($block['first']);
- }
- if (is_array($block['slider'])) {
- if ($this->currentPage - $window > 1) {
- $html .= $this->getDots();
- }
- $html .= $this->getUrlLinks($block['slider']);
- }
- if (is_array($block['last'])) {
- if ($this->lastPage - $this->currentPage > $window) {
- $html .= $this->getDots();
- }
- $html .= $this->getUrlLinks($block['last']);
- }
- return $html;
- }
-
- public function render() {
- $request = request();
- if ($this->simple || $request->isMobile()) {
- return sprintf(
- '%s %s',
- $this->getSimplePreviousButton(),
- $this->getSimpleNextButton()
- );
- } else {
- return sprintf(
- '%s <ul class="pagination pull-right">%s %s %s</ul>',
- $this->getInfo(),
- $this->getPreviousButton(),
- $this->getLinks(),
- $this->getNextButton()
- );
- }
- }
-
- protected function getAvailablePageWrapper($url, $page) {
- return '<li class="page-item"><a class="page-link" href="'.htmlentities($url).'">'.$page.'</a></li>';
- }
-
- protected function getDisabledTextWrapper($text) {
- return '<li class="page-item disabled"><span class="page-link">'.$text.'</span></li>';
- }
-
- protected function getActivePageWrapper($text) {
- return '<li class="active page-item disabled" ><span class="page-link">'.$text.'</span></li>';
- }
-
- protected function getDots() {
- return $this->getDisabledTextWrapper('...');
- }
-
- protected function getUrlLinks(array $urls) {
- $html = '';
- foreach ($urls as $page => $url) {
- $html .= $this->getPageLinkWrapper($url, $page);
- }
- return $html;
- }
-
- protected function getPageLinkWrapper($url, $page) {
- if ($page == $this->currentPage()) {
- return $this->getActivePageWrapper($page);
- }
- return $this->getAvailablePageWrapper($url, $page);
- }
- }
|