php 重写分页器 CLinkPager的实例
php重写分页器CLinkPager的实例
1、自定义的分页器类放在哪里?
有两个位置可以放,
第一种是放在protected/extensions中,在使用是import进来,或在config文件中import进来;
第二种是放在protected/components中,作为组件存在,不需要import
2、用派生方式是最好的
classMyPagerextendsCLinkPager
入口函数是:publicfunctionrun(),当显示分页器时run()被调用,里面的输出就会显示在相应位置;
其他的完全自定义,如果你不知道上一页、下一页、首页、尾页、总页数、当前页码等信息,可以参考CLinkPager的源码,yii/frameworks/web/widgets/pagers/CLinkPager.php
'. */ public$nextPageLabel; /** *@varstringthetextlabelforthepreviouspagebutton.Defaultsto'>'. */ public$lastPageLabel; /** *@varstringthetextshownbeforepagebuttons.Defaultsto'Gotopage:'. */ public$header; /** *@varstringthetextshownafterpagebuttons. */ public$footer=''; /** *@varmixedtheCSSfileusedforthewidget.Defaultstonull,meaning *usingthedefaultCSSfileincludedtogetherwiththewidget. *Iffalse,noCSSfilewillbeused.Otherwise,thespecifiedCSSfile *willbeincludedwhenusingthiswidget. */ public$cssFile; /** *@vararrayHTMLattributesforthepagercontainertag. */ public$htmlOptions=array(); /** *Initializesthepagerbysettingsomedefaultpropertyvalues. */ publicfunctioninit() { if($this->nextPageLabel===null) $this->nextPageLabel=Yii::t('yii','Next>'); if($this->prevPageLabel===null) $this->prevPageLabel=Yii::t('yii',' firstPageLabel===null) //$this->firstPageLabel=Yii::t('yii','< lastPageLabel===null) //$this->lastPageLabel=Yii::t('yii','Last>>'); if($this->header===null) $this->header=Yii::t('yii','Gotopage:'); if(!isset($this->htmlOptions['id'])) $this->htmlOptions['id']=$this->getId(); if(!isset($this->htmlOptions['class'])) $this->htmlOptions['class']='yiiPager'; } /** *Executesthewidget. *Thisoverridestheparentimplementationbydisplayingthegeneratedpagebuttons. */ publicfunctionrun() { $this->registerClientScript(); $buttons=$this->createPageButtons(); if(empty($buttons)) return; echo$this->header; //echoCHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons)); echoimplode("\n",$buttons); echo$this->footer; } /** *Createsthepagebuttons. *@returnarrayalistofpagebuttons(inHTMLcode). */ protectedfunctioncreatePageButtons() { if(($pageCount=$this->getPageCount())<=1) returnarray(); list($beginPage,$endPage,$ellipsis)=$this->getPageRange(); $currentPage=$this->getCurrentPage(false);//currentPageiscalculatedingetPageRange() $buttons=array(); //firstpage //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false); //prevpage if(($page=$currentPage-1)<0) $page=0; if($currentPage==0){ $buttons[]=" <上一頁"; }else{ $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false); } //internalpagesstart //first $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage); //middle if($ellipsis=='both'){ $buttons[]=" ..."; } for($i=$beginPage;$i<=$endPage;++$i){ if($ellipsis=='left'&&$i==$beginPage){ $buttons[]=" ..."; } $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage); if($ellipsis=='right'&&$i==$endPage){ $buttons[]=" ..."; } } if($ellipsis=='both'){ $buttons[]=" ..."; } //last $buttons[]=$this->createPageButton($pageCount,$pageCount-1,$this->internalPageCssClass,false,$i==$currentPage); //internalpagesend //nextpage if(($page=$currentPage+1)>=$pageCount-1) $page=$pageCount-1; if($currentPage==($pageCount-1)){ $buttons[]=" 下一頁>"; }else{ $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false); } //lastpage //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false); return$buttons; } /** *Createsapagebutton. *Youmayoverridethismethodtocustomizethepagebuttons. *@paramstring$labelthetextlabelforthebutton *@paraminteger$pagethepagenumber *@paramstring$classtheCSSclassforthepagebutton. *@paramboolean$hiddenwhetherthispagebuttonisvisible *@paramboolean$selectedwhetherthispagebuttonisselected *@returnstringthegeneratedbutton */ protectedfunctioncreatePageButton($label,$page,$class,$hidden,$selected) { if($hidden||$selected) $class.=''.($hidden?$this->hiddenPageCssClass:$this->selectedPageCssClass); if($selected){ $result="".++$page.""; }else{ $result=CHtml::link($label,$this->createPageUrl($page)); } return$result; } /** *@returnarraythebeginandendpagesthatneedtobedisplayed. */ protectedfunctiongetPageRange() { $currentPage=$this->getCurrentPage(); $pageCount=$this->getPageCount(); /*$beginPage=max(0,$currentPage-(int)($this->maxButtonCount/2)); if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { $endPage=$pageCount-1; $beginPage=max(0,$endPage-$this->maxButtonCount+1); }*/ if($pageCount>$this->maxButtonCount){ if($currentPage>4&&$currentPage<($pageCount-4)){ //print_r('a'); $beginPage=$currentPage-2; $endPage=$currentPage+2; $ellipsis='both'; }else{ $beginPage=max(1,$currentPage-(int)($this->maxButtonCount/2)); if($beginPage==1){ $ellipsis='right'; }else{ $ellipsis='left'; } if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { //print_r('b'); $endPage=$pageCount-2; $beginPage=max(1,$endPage-$this->maxButtonCount+1); }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){ //print_r('c'); $endPage=$pageCount-2; } } }else{ $beginPage=max(1,$currentPage-(int)($this->maxButtonCount/2)); if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { $endPage=$pageCount-2; $beginPage=max(1,$endPage-$this->maxButtonCount+1); } } returnarray($beginPage,$endPage,$ellipsis); } /** *Registerstheneededclientscripts(mainlyCSSfile). */ publicfunctionregisterClientScript() { if($this->cssFile!==false) self::registerCssFile($this->cssFile); } /** *RegisterstheneededCSSfile. *@paramstring$urltheCSSURL.Ifnull,adefaultCSSURLwillbeused. */ publicstaticfunctionregisterCssFile($url=null) { if($url===null) $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css'); Yii::app()->getClientScript()->registerCssFile($url); } }
3、调用方式
在View里的相应widget,定义pager的class为自定义的分页器类名即可,参考:
$this->widget('zii.widgets.CListView',array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view_t', 'pager'=>array( 'class'=>'MyPager', ) ));
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!