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中操作MySQL数据库的一些要注意的问题
Oct 09 PHP
Ajax PHP分页演示
Jan 02 PHP
IStream与TStream之间的相互转换
Aug 01 PHP
php DOS攻击实现代码(附如何防范)
May 29 PHP
记录mysql性能查询过程的使用方法
May 02 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(一)
Jun 23 PHP
php中fgetcsv()函数用法实例
Nov 28 PHP
php中数据库连接方式pdo和mysqli对比分析
Feb 25 PHP
详解WordPress开发中的get_post与get_posts函数使用
Jan 04 PHP
全面解析PHP操作Memcache基本函数
Jul 14 PHP
CentOS系统中PHP安装扩展的方式汇总
Apr 09 PHP
PHP合并两个或多个数组的方法
Jan 20 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
PHP+AJAX实现无刷新注册(带用户名实时检测)
2006/12/02 PHP
360通用php防护代码(使用操作详解)
2013/06/18 PHP
解决php表单重复提交实现方法
2015/09/29 PHP
CodeIgniter 完美解决URL含有中文字符串
2016/05/13 PHP
PHP中利用sleep函数实现定时执行功能实现代码
2016/08/25 PHP
Javascript中的数学函数
2007/04/04 Javascript
JavaScript的public、private和privileged模式
2009/12/28 Javascript
防止文件缓存的js代码
2013/01/10 Javascript
基于Jquery实现键盘按键监听
2014/05/11 Javascript
使用getBoundingClientRect方法实现简洁的sticky组件的方法
2016/03/22 Javascript
javascript函数的节流[throttle]与防抖[debounce]
2017/11/15 Javascript
javaScript 连接打印机,打印小票的实例
2017/12/29 Javascript
基于VuePress 轻量级静态网站生成器的实现方法
2018/04/17 Javascript
vue template中slot-scope/scope的使用方法
2018/09/06 Javascript
Vue指令v-for遍历输出JavaScript数组及json对象的常见方式小结
2019/02/11 Javascript
小程序显示弹窗时禁止下层的内容滚动实现方法
2019/03/20 Javascript
使用React代码动态生成栅格布局的方法
2020/05/24 Javascript
react antd表格中渲染一张或多张图片的实例
2020/10/28 Javascript
浅析python中SQLAlchemy排序的一个坑
2017/02/24 Python
python脚本替换指定行实现步骤
2017/07/11 Python
python实现决策树ID3算法的示例代码
2018/05/30 Python
目前最全的python的就业方向
2018/06/05 Python
Python之csv文件从MySQL数据库导入导出的方法
2018/06/21 Python
python 对key为时间的dict排序方法
2018/10/17 Python
Python读取excel指定列生成指定sql脚本的方法
2018/11/28 Python
python读取xlsx的方法
2018/12/25 Python
Python OpenCV利用笔记本摄像头实现人脸检测
2020/08/20 Python
python中threading开启关闭线程操作
2020/05/02 Python
Intimissimi德国网上商店:意大利知名内衣品牌
2018/04/03 全球购物
车间副主任岗位职责
2013/12/24 职场文书
学生会主席就职演讲稿
2014/01/14 职场文书
铲车司机岗位职责
2014/03/15 职场文书
政府法律服务方案
2014/06/14 职场文书
2015年科室工作总结
2015/04/10 职场文书
Python基于Opencv识别两张相似图片
2021/04/25 Python
windows系统安装配置nginx环境
2022/06/28 Servers