thinkphp 默认的分页样式很不好看,分页多了就会出现样式错乱,如何实现分页带有省略号呢?其实方法很简单。这个方法纯天然,无公害。
代码如下:
代码如下 | 复制代码 |
class IndexAction extends Action { public function index() { $M = M("Article_21"); $count = $M->count(); import("ORG.Util.Page"); $page = new Page($count, 10); //$showPage = $page->show();//Thinkphp原有的方法 $showPage = $page->shows();//自定义的方法 $list=$M->limit($page->firstRow, $page->listRows)->select();; $this->assign("page", $showPage); $this->assign("list",$list); $this->display(); } } |
找到Thinkphp核心包中的分页类
在下面添加一下代码:
代码如下 | 复制代码 |
public function shows() { $adjacents=2; if(0 == $this->totalRows) return ''; $p = $this->varPage; $nowCoolPage = ceil($this->nowPage/$this->rollPage); // 分析分页参数 if($this->url){ $depr = C('URL_PATHINFO_DEPR'); $url = rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__'; }else{ if($this->parameter && is_string($this->parameter)) { parse_str($this->parameter,$parameter); }elseif(is_array($this->parameter)){ $parameter = $this->parameter; }elseif(empty($this->parameter)){ unset($_GET[C('VAR_URL_PARAMS')]); $var = !empty($_POST)?$_POST:$_GET; if(empty($var)) { $parameter = array(); }else{ $parameter = $var; } } $parameter[$p] = '__PAGE__'; $url = U('',$parameter); } //上下翻页字符串 $upRow = $this->nowPage-1; $downRow = $this->nowPage+1; // 上一页 if ($upRow>0){ $pages.= "".$this->config['prev'].""; }else{ $pages.= "".$this->config['prev'].""; } //第一页 if($this->nowPage>($adjacents+1)) { $pages.= "1"; } // 添加省略号 if($this->nowPage>($adjacents+2)) { $pages.= "..."; } // 12345 $pmin = ($this->nowPage>$adjacents) ? ($this->nowPage-$adjacents) : 1; $pmax = ($this->nowPage<($this->totalPages-$adjacents)) ? ($this->nowPage+$adjacents) : $this->totalPages; for($i=$pmin; $i<=$pmax; $i++) { if($i==$this->nowPage) { $pages.= "".$i.""; }else{ $pages.= "".$i.""; } } // 添加省略号 if($this->nowPage < ($this->totalPages-$adjacents-1)) { $pages.= "..."; } // 最后一页 if($this->nowPage<($this->totalPages-$adjacents)) { $pages.= "totalPages,$url)."'>".$this->totalPages.""; } // 下一页 if ($downRow <= $this->totalPages){ $pages.= "".$this->config['next'].""; }else{ $pages.= "".$this->config['next'].""; } return $pages; } html代码:
{$page} |
效果图: