当前位置:首页 > CMS教程 > 其它CMS > 列表

Yii使用CLinkPager分页实例详解

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-23 12:00:52 浏览: 评论:0 

这篇文章主要介绍了Yii使用CLinkPager分页的方法,需要的朋友可以参考下

本文主要讲解了YII中使用CLinkPager分页的方法,这里我们采用物件的形式来定义分页:

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

具体代码如下:

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

定义CSS样式

  1. /** 
  2.  * 翻页样式 
  3.  */ 
  4. .page_blue{ 
  5.  margin: 3px; 
  6.  padding: 3px; 
  7.  text-align: center; 
  8.  font: 12px verdana, arial, helvetica, sans-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 !important; border:#156a9a 1px solid; background-color:#2b78a3 
  45. } 
  46.  
  47. ul.bluePager .selected a,ul.yiiPager bluePager .selected a 
  48. { 
  49.  color:#3aa1d0 !important; 
  50.  border: 1px 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中引用:

$this->widget('LinkPager', array('pages' => $pages,));

Tags: CLinkPager分页

分享到: