Yii使用CLinkPager分页实例详解
本文主要讲解了YII中使用CLinkPager分页的方法,这里我们采用物件的形式来定义分页:
首先在components中自定义LinkPager,并继承CLinkPager
具体代码如下:
<?php
/**
*CLinkPagerclassfile.
*
*@linkhttp://www.yiiframework.com/
*@copyrightCopyright©2008-2011YiiSoftwareLLC
*@licensehttp://www.yiiframework.com/license/
*/
/**
*CLinkPagerdisplaysalistofhyperlinksthatleadtodifferentpagesoftarget.
*
*@version$Id$
*@packagesystem.web.widgets.pagers
*@since1.0
*/
classLinkPagerextendsCLinkPager
{
constCSS_TOTAL_PAGE='total_page';
constCSS_TOTAL_ROW='total_row';
/**
*@varstringthetextlabelforthefirstpagebutton.Defaultsto'<<First'.
*/
public$totalPageLabel;
/**
*@varstringthetextlabelforthelastpagebutton.Defaultsto'Last>>'.
*/
public$totalRowLabel;
/**
*Createsthepagebuttons.
*@returnarrayalistofpagebuttons(inHTMLcode).
*/
protectedfunctioncreatePageButtons()
{
$this->maxButtonCount=8;
$this->firstPageLabel="首页";
$this->lastPageLabel='末页';
$this->nextPageLabel='下一页';
$this->prevPageLabel='上一页';
$this->header="";
if(($pageCount=$this->getPageCount())<=1)
returnarray();
list($beginPage,$endPage)=$this->getPageRange();
$currentPage=$this->getCurrentPage(false);//currentPageiscalculatedingetPageRange()
$buttons=array();
//firstpage
$buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);
//prevpage
if(($page=$currentPage-1)<0)
$page=0;
$buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);
//internalpages
for($i=$beginPage;$i<=$endPage;++$i)
$buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);
//nextpage
if(($page=$currentPage+1)>=$pageCount-1)
$page=$pageCount-1;
$buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);
//lastpage
$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;
}
protectedfunctioncreateTotalButton($label,$class,$hidden,$selected)
{
if($hidden||$selected)
$class.=''.($hidden?self::CSS_HIDDEN_PAGE:self::CSS_SELECTED_PAGE);
return'<liclass="'.$class.'">'.CHtml::label($label,false).'</li>';
}
/**
*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('application.components.views.LinkPager.pager').'.css');
Yii::app()->getClientScript()->registerCssFile($url);
}
}
定义CSS样式
/**
*翻页样式
*/
.page_blue{
margin:3px;
padding:3px;
text-align:center;
font:12pxverdana,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.bluePagerli,ul.yiiPagerli
{
display:inline;
}
ul.bluePagera:link,ul.yiiPagera:link,
ul.bluePagera:visited,ul.yiiPagera:visited,
ul.bluePager.total_pagelabel,ul.yiiPager.total_pagelabel,
ul.bluePager.total_rowlabel,ul.yiiPager.total_rowlabel
{
border:#ddd1pxsolid;
color:#888888!important;
padding:2px5px;
text-decoration:none;
}
ul.bluePager.pagea,ul.yiiPager.pagea
{
font-weight:normal;
}
ul.bluePagera:hover,ul.yiiPagera:hover
{
color:#FFF!important;border:#156a9a1pxsolid;background-color:#2b78a3
}
ul.bluePager.selecteda,ul.yiiPagerbluePager.selecteda
{
color:#3aa1d0!important;
border:1pxsolid#3aa1d0;
}
ul.bluePager.selecteda:hover,ul.yiiPager.selecteda:hover
{
color:#FFF!important;
}
ul.bluePager.hiddena,ul.yiiPager.hiddena
{
border:solid1px#DEDEDE;
color:#888888;
}
ul.bluePager.hidden,ul.yiiPager.hidden
{
display:none;
}
controller中操作:
//分页操作
$criteria=newCDbCriteria;
$criteria->order='idDESC';
$criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality');
$criteria->condition=$sql;
$total=Video::model()->count($criteria);
$pages=newCPagination($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,));
?>