Yii使用CLinkPager分页实例详解


Posted in PHP onJuly 23, 2014

本文主要讲解了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,));
 ?>
PHP 相关文章推荐
substr()函数中文版
Oct 09 PHP
攻克CakePHP系列三 表单数据增删改
Oct 22 PHP
php 不同编码下的字符串长度区分
Sep 26 PHP
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
Dec 25 PHP
thinkphp验证码显示不出来的解决方法
Mar 29 PHP
CI框架中集成CKEditor编辑器的教程
Jun 09 PHP
PHP函数实现从一个文本字符串中提取关键字的方法
Jul 01 PHP
CodeIgniter钩子用法实例详解
Jan 20 PHP
Linux(CentOS)下PHP扩展PDO编译安装的方法
Apr 07 PHP
PHP命令Command模式用法实例分析
Aug 08 PHP
PHP面向对象程序设计__tostring()和__invoke()用法分析
Jun 12 PHP
Laravel Reponse响应客户端示例详解
Sep 03 PHP
ThinkPHP单字母函数(快捷方法)使用总结
Jul 23 #PHP
PHP中的use关键字概述
Jul 23 #PHP
ThinkPHP实现将SESSION存入MYSQL的方法
Jul 22 #PHP
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
Jul 22 #PHP
ThinkPHP权限认证Auth实例详解
Jul 22 #PHP
ThinkPHP行为扩展Behavior应用实例详解
Jul 22 #PHP
qq登录,新浪微博登录接口申请过程中遇到的问题
Jul 22 #PHP
You might like
基于mysql的论坛(5)
2006/10/09 PHP
PHP中使用unset销毁变量并内存释放问题
2012/07/05 PHP
php 魔术常量详解及实例代码
2016/12/04 PHP
PHP示例演示发送邮件给某个邮箱
2019/04/03 PHP
javascript 数组学习资料收集
2010/04/11 Javascript
JavaScript中把数字转换为字符串的程序代码
2013/06/19 Javascript
jQuery插件开发的两种方法及$.fn.extend的详解
2014/01/16 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
完美解决IE不支持Data.parse()的问题
2016/11/24 Javascript
Vue.js路由组件vue-router使用方法详解
2016/12/02 Javascript
vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)
2017/04/22 Javascript
jQuery ajax动态生成table功能示例
2017/06/14 jQuery
jquery ajax异步提交表单数据的方法
2017/10/27 jQuery
bootstrap select下拉搜索插件使用方法详解
2017/11/23 Javascript
create-react-app 修改为多入口编译的方法
2018/08/01 Javascript
基于axios 解决跨域cookie丢失的问题
2018/09/26 Javascript
JavaScript静态作用域和动态作用域实例详解
2019/06/17 Javascript
js实现纯前端压缩图片
2020/11/16 Javascript
[08:44]和酒神一起战斗 DOTA2教你做大人
2014/03/27 DOTA
[01:18]一目了然!DOTA2DotA快捷操作对比第一弹
2014/07/01 DOTA
[40:19]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第二场 12.18
2020/12/19 DOTA
简单谈谈Python的pycurl模块
2018/04/07 Python
pyqt5的QComboBox 使用模板的具体方法
2018/09/06 Python
Python帮你识破双11的套路
2019/11/11 Python
详解Python中的format格式化函数的使用方法
2019/11/20 Python
python如何求圆的面积
2020/07/01 Python
python两个list[]相加的实现方法
2020/09/23 Python
印度领先的在线时尚商店:Koovs
2016/08/28 全球购物
美国最大的城市服装和运动鞋零售商:Jimmy Jazz
2016/11/19 全球购物
初一地理教学反思
2014/01/16 职场文书
优秀学生事迹材料
2014/02/08 职场文书
2014年教师政治学习材料
2014/06/02 职场文书
企业总经理助理岗位职责
2014/09/12 职场文书
三方股份合作协议书
2014/10/13 职场文书
父亲节活动总结
2015/02/12 职场文书
彻底卸载VMware虚拟机的超详细步骤记录
2022/07/15 Servers