欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Yii使用CLinkPager分页实例详解

程序员文章站 2022-12-28 11:14:08
本文主要讲解了yii中使用clinkpager分页的方法,这里我们采用物件的形式来定义分页: 首先在components中自定义linkpager,并继承clinkpag...

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

首先在components中自定义linkpager,并继承clinkpager

具体代码如下:

<?php
/**
 * clinkpager class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright copyright © 2008-2011 yii software llc
 * @license http://www.yiiframework.com/license/
 */

/**
 * clinkpager displays a list of hyperlinks that lead to different pages of target.
 *
 * @version $id$
 * @package system.web.widgets.pagers
 * @since 1.0
 */
class linkpager extends clinkpager
{
 const css_total_page='total_page';
 const css_total_row='total_row';
 
 /**
 * @var string the text label for the first page button. defaults to '<< first'.
 */
 public $totalpagelabel;
 /**
 * @var string the text label for the last page button. defaults to 'last >>'.
 */
 public $totalrowlabel;
 
 /**
 * creates the page buttons.
 * @return array a list of page buttons (in html code).
 */
 protected function createpagebuttons()
 {
 

    $this->maxbuttoncount=8; 
    $this->firstpagelabel="首页";
    $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 ? self::css_hidden_page : self::css_selected_page);
 return '<li class="'.$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,));
 ?>