第三节 ThinkPHP 自定义分页模板

最后更新于:2022-04-02 01:46:59

>[success] # 默认分页模板 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/e62f97e2699a9355038e4fdf8645ad24_586x316.png) >[success] # 自定义分页模板 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/ff14988e37b002a6f3e2379b3c14b305_666x321.png) ```php currentPage() <= 1) { return $this->getDisabledTextWrapper($str); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $str); } //TODO : 上一页 protected function getPreviousButton($text = "«") { if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url( $this->currentPage() - 1 ); return $this->getPageLinkWrapper($url, $text); } //TODO : 页码 protected function getLinks() { if ($this->total > $this->listRows) { if ($this->lastPage < $this->bnumber) { return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage)); } else { $min = 1; if ($this->currentPage > $this->bnumber / 2) $min = $this->currentPage - floor($this->bnumber / 2); if ($this->lastPage - $this->currentPage < $this->bnumber / 2) $min = $this->lastPage - $this->bnumber + 1; return $this->getUrlLinks($this->getUrlRange($min, $min + $this->bnumber - 1)); } } } //TODO : 下一页 protected function getNextButton($text = '»') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } //TODO : 末页 protected function getlastButton($text = '') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); } //TODO : 渲染页 public function render() { //数据是否足够分页 if ($this->hasPages()) { return sprintf( '
    %s %s %s %s %s
', $this->getfirstButton('首页'), $this->getPreviousButton('上一页'), $this->getLinks(), $this->getNextButton('下一页'), $this->getlastButton('末页') ); } } //TODO : 生成禁用按钮 protected function getDisabledTextWrapper($text) { return '
  • ' . $text . '
  • '; } //TODO : 生成普通按钮 protected function getPageLinkWrapper($url, $page) { if ($page == $this->currentPage()) { return $this->getActivePageWrapper($page); } return $this->getAvailablePageWrapper($url, $page); } //TODO : 生成当前页按钮 protected function getActivePageWrapper($text) { return '
  • ' . $text . '
  • '; } //TODO : 可点击按钮 protected function getAvailablePageWrapper($url, $page) { return '
  • ' . $page . '
  • '; } //TODO : 批量生成页码按钮 protected function getUrlLinks(array $urls) { $html = ''; foreach ($urls as $page => $url) { $html .= $this->getPageLinkWrapper($url, $page); } return $html; } } ``` * 将 TpPageHelper.php 放在extend\tool下 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/d2f9796713cadd91d6fc66d371d7c634_225x73.png) * 修改默认模板 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/0e0f5f5427c7c5fedbf205d510b00f5d_850x305.png) > ### 项目源码下载 - Tip:公众号回复【025】获取源代码 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/de/d6/ded6e495eb8a39d2c7a8b0547b329d19_430x430.jpg)
    ';