// +---------------------------------------------------------------------- namespace cmf\paginator; use think\Paginator; class Bootstrap extends Paginator { /** * @return string */ 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 = << 显示第 {$_start} 到第 {$_end} 条记录,总共 {$this->total()} 条记录 每页显示 条记录
跳转到 GO
EOF; return $_txt; } /** * 上一页按钮 * * @param string $text * * @return string */ 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); } /** * 下一页按钮 * * @param string $text * * @return string */ 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); } /** * 上一页按钮 * * @param string $text * * @return string */ 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 ''; } $url = $this->url($this->currentPage() - 1); return ''; } /** * 下一页按钮 * * @param string $text * * @return string */ 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 ''; } $url = $this->url($this->currentPage() + 1); return ''; } /** * 页码按钮 * * @return string */ 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; } /** * 渲染分页html * * @return mixed */ public function render() { // if ($this->hasPages()) { $request = request(); if ($this->simple || $request->isMobile()) { return sprintf( '%s %s', $this->getSimplePreviousButton(), $this->getSimpleNextButton() ); } else { return sprintf( '%s ', $this->getInfo(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() ); } // } } /** * 生成一个可点击的按钮 * * @param string $url * @param int $page * * @return string */ protected function getAvailablePageWrapper($url, $page) { return '
  • '.$page.'
  • '; } /** * 生成一个禁用的按钮 * * @param string $text * * @return string */ protected function getDisabledTextWrapper($text) { return '
  • '.$text.'
  • '; } /** * 生成一个激活的按钮 * * @param string $text * * @return string */ protected function getActivePageWrapper($text) { return '
  • '.$text.'
  • '; } /** * 生成省略号按钮 * * @return string */ protected function getDots() { return $this->getDisabledTextWrapper('...'); } /** * 批量生成页码按钮. * * @param array $urls * * @return string */ protected function getUrlLinks(array $urls) { $html = ''; foreach ($urls as $page => $url) { $html .= $this->getPageLinkWrapper($url, $page); } return $html; } /** * 生成普通页码按钮 * * @param string $url * @param int $page * * @return string */ protected function getPageLinkWrapper($url, $page) { if ($page == $this->currentPage()) { return $this->getActivePageWrapper($page); } return $this->getAvailablePageWrapper($url, $page); } }