yii的分页类CLinkPager默认是不支持显示共x页的,那么现在需求来了,要在分页的后面显示共多少页,怎么办喃?我们来看解决办法
1、默认的CLinkPager显示的效果
上面这里写了css的样式哈,我们来看pager代码:
<div class="page-link"><?php $this->widget('CLinkPager',array('header' => '','firstPageLabel' => '首页','lastPageLabel' => '尾页','prevPageLabel' => '<','nextPageLabel' => '>','pages' => $pages,'maxButtonCount'=>5,'htmlOptions' => array('class' => 'page-link'), //分页要使用的css样式));?></div>
2、我们来看想要的分页类效果
也就是说后面增加显示了共多少页,这个怎么做到的喃?这里我稍微小小的扩展了一下widget组件CLinkPager,看上去也是非常的狠狠简单呐,废话不多少,来来先看代码:
<?php/*** 分页组建ClinkPager扩展* @description page-tab-tog为分页的样式class* @author <[<xm 杭州>]>* @time 2016-01-29* @example* <div class="page-tab-tog">* <?php $this->widget('MLinkPager',array(* 'header' => '',* 'firstPageLabel' => '首页',* 'lastPageLabel' => '尾页',* 'prevPageLabel' => '<',* 'nextPageLabel' => '>',* 'pages' => $pages,* 'maxButtonCount'=>5,* 'htmlOptions' => array('class' => 'page-tab-tog'),* ));?>* </div>*/class MLinkPager extends CLinkPager{//设置为true的时候,显示共X页,$this->forceTotalPage值优先该值public $mCountPage = false;//是否强制显示共x页,设置为true时,$this->mCountPage和$this->getPageRange()无效public $forceTotalPage = false;public function init(){}public function run(){$this->registerClientScript();$buttons=$this->createPageButtons();list($beginPage,$endPage)=$this->getPageRange();if ($this->forceTotalPage){$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页');}else{if ($this->mCountPage && $endPage > 0){$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页');}}if(empty($buttons))return;echo $this->header;echo CHtml::tag('div',$this->htmlOptions,implode("/n",$buttons));echo $this->footer;}}
有人说了,一看那么一堆代码,头疼,你这玩意怎么能以最快的速度见到效果呢?来来我们继续看怎么使用,首先呢,你需要先把上面的扩展MLinkPager原封不动的拷贝到本地的components目录下的MlinkPager文件里,什么,你没有这个文件,自己创建,^~^!好了以后咱们来看下view里面是怎么使用的,那是简单的不能再过于简单了。
<div class="page-tab-tog"><?php $this->widget('MLinkPager',array('header' => '','firstPageLabel' => '首页','lastPageLabel' => '尾页','prevPageLabel' => '<','nextPageLabel' => '>','pages' => $pages,'maxButtonCount'=>5,'mCountPage' => true, //!!!注意看这里,加一行代码就ok了'htmlOptions' => array('class' => 'page-tab-tog'),));?></div>
新闻热点
疑难解答