YII CLinkPager分页

这里我们采用物件的形式来定义分页:

在components中自定义LinkPager,并继承CLinkPager

  1.   
  1. <?php  
  2. /** 
  3.  * CLinkPager class file. 
  4.  * 
  5.  * @author Qiang Xue <qiang.xue@gmail.com> 
  6.  * @link http://www.yiiframework.com/ 
  7.  * @copyright Copyright © 2008-2011 Yii Software LLC 
  8.  * @license http://www.yiiframework.com/license/ 
  9.  */  
  10.   
  11. /** 
  12.  * CLinkPager displays a list of hyperlinks that lead to different pages of target. 
  13.  * 
  14.  * @author Qiang Xue <qiang.xue@gmail.com> 
  15.  * @version $Id$ 
  16.  * @package system.web.widgets.pagers 
  17.  * @since 1.0 
  18.  */  
  19. class LinkPager extends CLinkPager  
  20. {  
  21.     const CSS_TOTAL_PAGE='total_page';  
  22.     const CSS_TOTAL_ROW='total_row';  
  23.       
  24.     /** 
  25.      * @var string the text label for the first page button. Defaults to '<< First'. 
  26.      */  
  27.     public $totalPageLabel;  
  28.     /** 
  29.      * @var string the text label for the last page button. Defaults to 'Last >>'. 
  30.      */  
  31.     public $totalRowLabel;  
  32.       
  33.     /** 
  34.      * Creates the page buttons. 
  35.      * @return array a list of page buttons (in HTML code). 
  36.      */  
  37.     protected function createPageButtons()  
  38.     {  
  39.           
  40.   
  41.         $this->maxButtonCount=8;    
  42.         $this->firstPageLabel="首页";  
  43.         $this->lastPageLabel='末页';   
  44.         $this->nextPageLabel='下一页';  
  45.         $this->prevPageLabel='上一页';   
  46.         $this->header="";  
  47.           
  48.         if(($pageCount=$this->getPageCount())<=1)  
  49.                 return array();  
  50.           
  51.         list($beginPage,$endPage)=$this->getPageRange();  
  52.         $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()  
  53.         $buttons=array();  
  54.           
  55.         // first page  
  56.         $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);  
  57.       
  58.         // prev page  
  59.         if(($page=$currentPage-1)<0)  
  60.                 $page=0;  
  61.         $buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);  
  62.       
  63.         // internal pages  
  64.         for($i=$beginPage;$i<=$endPage;++$i)  
  65.                 $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);  
  66.       
  67.         // next page  
  68.         if(($page=$currentPage+1)>=$pageCount-1)  
  69.                 $page=$pageCount-1;  
  70.         $buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);  
  71.       
  72.         // last page  
  73.         $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);  
  74.           
  75.         // 页数统计  
  76.         $buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false);  
  77.           
  78.         // 条数统计  
  79.         $buttons[]=$this->createTotalButton("共{$this->getItemCount()}条",self::CSS_TOTAL_ROW,false,false);  
  80.       
  81.         return $buttons;  
  82.     }  
  83.       
  84.     protected function createTotalButton($label,$class,$hidden,$selected)  
  85.     {  
  86.         if($hidden || $selected)  
  87.                 $class.=' '.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);  
  88.         return '<li class="'.$class.'">'.CHtml::label($label,false).'</li>';  
  89.     }  
  90.       
  91.     /** 
  92.      * Registers the needed client scripts (mainly CSS file). 
  93.      */  
  94.     public function registerClientScript()  
  95.     {  
  96.         if($this->cssFile!==false)  
  97.             self::registerCssFile($this->cssFile);  
  98.     }  
  99.       
  100.     /** 
  101.      * Registers the needed CSS file. 
  102.      * @param string $url the CSS URL. If null, a default CSS URL will be used. 
  103.      */  
  104.     public static function registerCssFile($url=null)  
  105.     {  
  106.         if($url===null)  
  107.             $url=CHtml::asset(Yii::getPathOfAlias('application.components.views.LinkPager.pager').'.css');  
  108.         Yii::app()->getClientScript()->registerCssFile($url);  
  109.     }  
  110. }  

定义CSS样式

  1. /** 
  2.  * 翻页样式 
  3.  */  
  4. .page_blue{  
  5.     margin3px;  
  6.     padding3px;  
  7.     text-aligncenter;  
  8.     font12px verdanaarialhelveticasans-serif;  
  9. }  
  10. ul.bluePager,ul.yiiPager  
  11. {  
  12.     font-size:11px;  
  13.     border:0;  
  14.     margin:0;  
  15.     padding:0;  
  16.     line-height:100%;  
  17.     display:inline;  
  18.     text-aligin:center;  
  19. }  
  20.   
  21. ul.bluePager li,ul.yiiPager li  
  22. {  
  23.     display:inline;  
  24. }  
  25.   
  26. ul.bluePager a:link,ul.yiiPager a:link,  
  27. ul.bluePager a:visited,ul.yiiPager a:visited,  
  28. ul.bluePager .total_page label,ul.yiiPager .total_page label,  
  29. ul.bluePager .total_row label,ul.yiiPager .total_row label  
  30. {  
  31.     border#ddd 1px solid;  
  32.     color#888888 !important;  
  33.     padding:2px 5px;  
  34.     text-decoration:none;  
  35. }  
  36.   
  37. ul.bluePager .page a,ul.yiiPager .page a  
  38. {  
  39.     font-weight:normal;  
  40. }  
  41.   
  42. ul.bluePager a:hover,ul.yiiPager a:hover  
  43. {  
  44.     color:#FFF !importantborder:#156a9a 1px solidbackground-color:#2b78a3  
  45. }  
  46.   
  47. ul.bluePager .selected a,ul.yiiPager bluePager .selected a  
  48. {  
  49.     color:#3aa1d0 !important;  
  50.     border1px solid #3aa1d0;  
  51. }  
  52.   
  53. ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover  
  54. {  
  55.     color:#FFF !important;  
  56. }  
  57.   
  58. ul.bluePager .hidden a,ul.yiiPager .hidden a  
  59. {  
  60.     border:solid 1px #DEDEDE;  
  61.     color:#888888;  
  62. }  
  63.   
  64. ul.bluePager .hidden,ul.yiiPager .hidden  
  65. {  
  66.     display:none;  
  67. }  
controller中操作

  1. //分页操作  
  2.             $criteria=new CDbCriteria;  
  3.             $criteria->order='id DESC';  
  4.             $criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality');  
  5.             $criteria->condition=$sql;  
  6.             $total = Video::model()->count($criteria);  
  7.               
  8.             $pages = new CPagination($total);             
  9.             $pages->pageSize=self::PAGE_SIZE;  
  10.             $pages->applyLimit($criteria);  
  11.                           
  12.             $list = Video::model()->findAll($criteria);  
  13.               
  14.             $title = CommonClass::model()->find(array(  
  15.                 'select'=>array('cname'),  
  16.                 'condition'=>'id = '.$id,  
  17.             ));           
  18.               
  19.             $this->render('application.views.video.list',array(  
  20.                 'array'=>$array,  
  21.                 'arr'=>$arr,  
  22.                 'result'=>$result,  
  23.                 'list'=>$list,  
  24.                 'pages'=>$pages,  
  25.                 'title'=>$title,  
  26.             ));  

在views/video/list.php中引用:

  1. <?php $this->widget('LinkPager'array('pages' => $pages,)); ?>  

效果如图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值