Home> PHP Framework> ThinkPHP> body text

thinkphp custom paging

Release: 2020-04-10 09:02:17
forward
3017 people have browsed it

ThinkPHP5.1 has built-in paging implementation. It is very simple to add paging output function to the data. You can directly call the paginate method when querying the Db class. This article introduces how to customize paging styles in thinkphp.

thinkphp custom paging

thinkphp5.1 has a very convenient paging class. You can use the render method to render the paging html code

But the "<<" One page and the next page such as ">>" sometimes cannot meet the changing needs of the project. It is necessary to define the paging display yourself, such as

Home Page Previous Page 1 2 3 ... 7 8 Next page Last page

In this way, however, the official manual does not mention the method of customizing the paging style. At first, I simply replaced the paging html with the text of the previous page and the next page

Later I found that I can define a class myself to complete this requirement. First, I need to create paginate.php in the config directory, the file content

'app\index\pager\gcudPager'//自己的分页类可以随便放,只要命名空间写对 ];
Copy after login

Then copy "project directory\thinkphp\library\think\ paginator\driver\Bootstrap.php" to any location, change the namespace, and change the type of paginate.php to the corresponding namespace. For example, I copied the file to "project directory\application\index\pager\gcudPager. php", the above type also corresponds to this path, then change the namespace to "app\index\pager", and change the corresponding class name to gcudPager, so that you can define the form of paging by yourself

I implemented the homepage based on the previous page, copied its code, and modified it slightly

/**首页按钮 * @param string $text * @return string */ protected function GetFirstButton($text='首页'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); }
Copy after login

The logic is very simple, that is, to determine the current page number, and manually set the page number variable to 1, and at the same time You can copy the code of the next page and change it to the last page

/**末页按钮 * @param string $text * @return string */ protected function GetLastButton($text='末页'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); }
Copy after login

Other previous page and next page are too simple to change the text. The render function part needs to add the homepage and last page buttons

/** * 渲染分页html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
    %s %s
', $this->getPreviousButton(), $this->getNextButton() ); } else { return sprintf( '
%s %s %s %s %s
', $this->GetFirstButton(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton(), $this->GetLastButton() ); } } }
Copy after login

That’s it. No need to change the calling part at all. Finally, put the complete code

         // +---------------------------------------------------------------------- namespace app\index\pager; use think\Paginator; class gcudPager extends Paginator { /**首页按钮 * @param string $text * @return string */ protected function GetFirstButton($text='首页'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); } /** * 上一页按钮 * @param string $text * @return string */ protected function getPreviousButton($text = "上一页") { 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 GetLastButton($text='末页'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); } /** * 下一页按钮 * @param string $text * @return string */ protected function getNextButton($text = '下一页') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } /** * 页码按钮 * @return string */ protected function getLinks() { if ($this->simple) { return ''; } $block = [ 'first' => null, 'slider' => null, 'last' => null, ]; $side = 3; $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 + 2), $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'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['slider']); } if (is_array($block['last'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['last']); } return $html; } /** * 渲染分页html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
    %s %s
', $this->getPreviousButton(), $this->getNextButton() ); } else { return sprintf( '
%s %s %s %s %s
', $this->GetFirstButton(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton(), $this->GetLastButton() ); } } } /** * 生成一个可点击的按钮 * * @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 ($this->currentPage() == $page) { return $this->getActivePageWrapper($page); } return $this->getAvailablePageWrapper($url, $page); } }
Copy after login

Recommended tutorial:thinkphp tutorial

The above is the detailed content of thinkphp custom paging. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:oschina.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!