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 相关文章推荐
PHP 字符串操作入门教程
Dec 06 PHP
Zend 输出产生XML解析错误
Mar 03 PHP
Ajax+PHP 边学边练 之二 实例
Nov 24 PHP
PHP 处理TXT文件(打开/关闭/检查/读取)
May 13 PHP
浅析php变量作用域的一些问题
Aug 08 PHP
Windows下的PHP安装文件线程安全和非线程安全的区别
Apr 23 PHP
PHP学习笔记之字符串编码的转换和判断
May 22 PHP
学习php设计模式 php实现门面模式(Facade)
Dec 07 PHP
Yii+upload实现AJAX上传图片的方法
Jul 13 PHP
thinkphp项目如何自定义微信分享描述内容
Feb 20 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
Feb 06 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
Feb 27 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
php5 pdo新改动加载注意事项
2008/09/11 PHP
PHP中Date()时间日期函数的使用方法小结
2011/04/20 PHP
解析PHP将对象转换成数组的方法(兼容多维数组类型)
2013/06/21 PHP
php实现zip文件解压操作
2015/11/03 PHP
php实现的操作excel类详解
2016/01/15 PHP
Jquery中获取iframe的代码
2011/01/11 Javascript
[JSF]使用DataModel处理表行事件的实例代码
2013/08/05 Javascript
下拉列表选择项的选中在不同浏览器中的兼容性问题探讨
2013/09/18 Javascript
js传参数受特殊字符影响错误的解决方法
2013/10/21 Javascript
Javascript Objects详解
2014/09/04 Javascript
javascript中递归函数用法注意点
2015/07/30 Javascript
jQuery实现带水平滑杆的焦点图动画插件
2016/03/08 Javascript
jQuery使用中可能被XSS攻击的一些危险环节提醒
2016/05/24 Javascript
JS 拦截全局ajax请求实例解析
2016/11/29 Javascript
JS继承与闭包及JS实现继承的三种方式
2017/10/15 Javascript
javascript计算对象长度的方法
2017/10/25 Javascript
基于jquery实现的tab选项卡功能示例【附源码下载】
2019/06/10 jQuery
微信小程序拼接图片链接无底洞深入探究
2019/09/03 Javascript
vue.js+elementUI实现点击左右箭头切换头像功能(类似轮播图效果)
2019/09/05 Javascript
jQuery插件simplePagination的使用方法示例
2020/04/28 jQuery
Python中字符串的修改及传参详解
2016/11/30 Python
利用python实现xml与数据库读取转换的方法
2017/06/17 Python
python 读写中文json的实例详解
2017/10/29 Python
解决Python获取字典dict中不存在的值时出错问题
2018/10/17 Python
解决Pycharm调用Turtle时 窗口一闪而过的问题
2019/02/16 Python
Python实现的删除重复文件或图片功能示例【去重】
2019/04/23 Python
python manage.py runserver流程解析
2019/11/08 Python
美国著名童装品牌:OshKosh B’gosh
2016/08/05 全球购物
中国央视网签名寄语
2014/01/18 职场文书
大学新生入学教育方案
2014/05/16 职场文书
员工安全承诺书
2014/05/22 职场文书
承诺书范文
2014/06/03 职场文书
委托函范文
2015/01/29 职场文书
出国留学自荐信模板
2015/03/06 职场文书
庆祝教师节新闻稿
2015/07/17 职场文书
详解使用内网穿透工具Ngrok代理本地服务
2022/03/31 Servers