网络编程 
首页 > 网络编程 > 浏览文章

Yii使用CLinkPager分页实例详解

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

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

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

具体代码如下:

<"首页";
    $this->lastPageLabel='末页'; 
    $this->nextPageLabel='下一页';
    $this->prevPageLabel='上一页'; 
    $this->header="";
 
 if(($pageCount=$this->getPageCount())<=1)
  return array();
 
 list($beginPage,$endPage)=$this->getPageRange();
 $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
 $buttons=array();
 
 // first page
 $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);

 // prev page
 if(($page=$currentPage-1)<0)
  $page=0;
 $buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);

 // internal pages
 for($i=$beginPage;$i<=$endPage;++$i)
  $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);

 // next page
 if(($page=$currentPage+1)>=$pageCount-1)
  $page=$pageCount-1;
 $buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);

 // last page
 $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);
 
 // 页数统计
 $buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false);
 
 // 条数统计
 $buttons[]=$this->createTotalButton("共{$this->getItemCount()}条",self::CSS_TOTAL_ROW,false,false);

 return $buttons;
 }
 
 protected function createTotalButton($label,$class,$hidden,$selected)
 {
 if($hidden || $selected)
  $class.=' '.($hidden "'.$class.'">'.CHtml::label($label,false).'</li>';
 }
 
 /**
 * Registers the needed client scripts (mainly CSS file).
 */
 public function registerClientScript()
 {
 if($this->cssFile!==false)
  self::registerCssFile($this->cssFile);
 }
 
 /**
 * Registers the needed CSS file.
 * @param string $url the CSS URL. If null, a default CSS URL will be used.
 */
 public static function registerCssFile($url=null)
 {
 if($url===null)
  $url=CHtml::asset(Yii::getPathOfAlias('application.components.views.LinkPager.pager').'.css');
 Yii::app()->getClientScript()->registerCssFile($url);
 }
}

定义CSS样式

/**
 * 翻页样式
 */
.page_blue{
 margin: 3px;
 padding: 3px;
 text-align: center;
 font: 12px verdana, arial, helvetica, sans-serif;
}
ul.bluePager,ul.yiiPager
{
 font-size:11px;
 border:0;
 margin:0;
 padding:0;
 line-height:100%;
 display:inline;
 text-aligin:center;
}

ul.bluePager li,ul.yiiPager li
{
 display:inline;
}

ul.bluePager a:link,ul.yiiPager a:link,
ul.bluePager a:visited,ul.yiiPager a:visited,
ul.bluePager .total_page label,ul.yiiPager .total_page label,
ul.bluePager .total_row label,ul.yiiPager .total_row label
{
 border: #ddd 1px solid;
 color: #888888 !important;
 padding:2px 5px;
 text-decoration:none;
}

ul.bluePager .page a,ul.yiiPager .page a
{
 font-weight:normal;
}

ul.bluePager a:hover,ul.yiiPager a:hover
{
 color:#FFF !important; border:#156a9a 1px solid; background-color:#2b78a3
}

ul.bluePager .selected a,ul.yiiPager bluePager .selected a
{
 color:#3aa1d0 !important;
 border: 1px solid #3aa1d0;
}

ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover
{
 color:#FFF !important;
}

ul.bluePager .hidden a,ul.yiiPager .hidden a
{
 border:solid 1px #DEDEDE;
 color:#888888;
}

ul.bluePager .hidden,ul.yiiPager .hidden
{
 display:none;
}

controller中操作:

//分页操作
$criteria=new CDbCriteria;
$criteria->order='id DESC';
$criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality');
$criteria->condition=$sql;
$total = Video::model()->count($criteria);

$pages = new CPagination($total);  
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);
  
$list = Video::model()->findAll($criteria);

$title = CommonClass::model()->find(array(
 'select'=>array('cname'),
 'condition'=>'id = '.$id,
));  

$this->render('application.views.video.list',array(
 'array'=>$array,
 'arr'=>$arr,
 'result'=>$result,
 'list'=>$list,
 'pages'=>$pages,
 'title'=>$title,
));

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

<?php
 $this->widget('LinkPager', array('pages' => $pages,));
 ?>
上一篇:PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
下一篇:ThinkPHP单字母函数(快捷方法)使用总结
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网