1、先在顶部引入use think\paginator\driver\Page;
2、使用下例代码
$pageNumber = input('page')? input('page'):'0';//客户端传过来的分页 if($pageNumber > 0){ $pageNumber_one = $pageNumber-1; }else{ $pageNumber_one = 0; } $limit = 20;//每页显示条数; $offset= $pageNumber_one*$limit;//查询偏移值//查询数 $list = Db::query("SELECT * FROM `表名` LIMIT $offset,$limit "); $count_data = Db::query('SELECT count(*) FROM `表名` '); $count = $count_data['0']['count(*)'];//组合分页数据格式 $this->assign('count',$count); //组合分页数据格式 $pagernator = Page::make($list,$limit,$pageNumber,$count,false,['path'=>Page::getCurrentPath(),'query'=>request()->param()]); $this->assign('datalist',$list); $this->assign('page',$pagernator->render());
3、think\paginator\driver\Page.php的代码如下
<?php namespace think\paginator\driver; use think\Paginator; class Page extends Paginator { //首页 protected function home($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 getNextButton($text = '下一页') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } //尾页 protected function last($text = '尾页') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage); 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->lastPage > 1) { if ($this->hasPages()) { if ($this->simple) { return sprintf( '<div class="pagination">%s %s %s %s</div>', $this->home(), $this->getPreviousButton(), $this->getNextButton(), $this->last() ); } else { return sprintf( '<div class="pagination">%s %s %s %s %s</div>', $this->home(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton(), $this->last() ); } } } } /** * 生成一个可点击的按钮 * * @param string $url * @param int $page * @return string */ protected function getAvailablePageWrapper($url, $page) { return '<a href="' . htmlentities($url) . '" title="第'. $page .'页" >' . $page . '</a>'; } /** * 生成一个禁用的按钮 * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<span>' . $text . '</span>'; } /** * 生成一个激活的按钮 * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<a href="" class="cur">' . $text . '</a>'; } /** * 生成省略号按钮 * * @return string */ protected function getDots() { //return $this->getDisabledTextWrapper('...'); } /** * 批量生成页码按钮. * * @param array $urls * @return string */ protected function getUrlLinks(array $urls) { $html = ''; $i = 1; foreach ($urls as $page => $url) { if($this->currentPage() == 0){ if($i == 1) { $html .= $this->getActivePageWrapper(1); }else{ $html .= $this->getPageLinkWrapper($url, $page); } }else{ $html .= $this->getPageLinkWrapper($url, $page); } $i++; } 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); } }
4、分页按钮样式
.pagination span{ margin:0; cursor:pointer } .pagination{ padding:0; margin:20px 0;text-align: center; } .pagination a{ margin-right:10px; padding:6px 12px; border:1px #cccccc solid; background:#fff; text-decoration:none; color:#808080; font-size:12px; line-height:24px; } .pagination a:hover{ color:#fc6d41; background: white; border:1px #fc6d41 solid; } .pagination a.cur{ border:1px #fc6d41 solid; background:#fc6d41; color:#fff;padding:6px 12px; } .pagination span{ padding:6px 12px; font-size:12px; line-height:24px; color:#bbb; border:1px #ccc solid; background:#fcfcfc; margin-right:8px; } .pagination span.pageRemark{ border-style:none; background:none; margin-right:0px; padding:4px 0px; color:#666; } .pagination span.pageRemark b{ color:red; } .pagination span.pageEllipsis{ border-style:none; background:none; padding:4px 0px; color:#808080; }