Home>Article>PHP Framework> thinkphp custom paging
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.
thinkphp5.1 has a very convenient paging class. You can use the render method to render the paging html code
But the "4693d65125aed9ded4029465c52a6076>" 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'//自己的分页类可以随便放,只要命名空间写对 ];
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); }
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); }
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( '
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( '
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!