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 相关文章推荐
一键删除顽固的空文件夹 软件下载
Jan 26 PHP
PHP curl模拟浏览器采集阿里巴巴的实现代码
Apr 20 PHP
关于php操作mysql执行数据库查询的一些常用操作汇总
Jun 24 PHP
解析php做推送服务端实现ios消息推送
Jul 01 PHP
学习php过程中的一些注意点的总结
Oct 25 PHP
php stripslashes和addslashes的区别
Feb 03 PHP
PHP中上传多个文件的表单设计例子
Nov 19 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
CodeIgniter常用知识点小结
May 26 PHP
php生成与读取excel文件
Oct 14 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
Sep 03 PHP
PHP实现对数字分隔加千分号的方法
Mar 18 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中强制下载文件的代码(解决了IE下中文文件名乱码问题)
2011/05/09 PHP
php生成zip压缩文件的方法详解
2013/06/09 PHP
easyui的tabs update正确用法分享
2014/03/21 PHP
php中switch与ifelse的效率区别及适用情况分析
2015/02/12 PHP
Thinkphp3.2.3分页使用实例解析
2016/07/28 PHP
PHP导出带样式的Excel示例代码
2016/08/28 PHP
php 微信公众平台开发模式实现多客服的实例代码
2016/11/07 PHP
PHP编程获取各个时间段具体时间的方法
2017/05/26 PHP
Eclipse PHPEclipse 配置的具体步骤
2017/08/08 PHP
laravel使用数据库测试注意事项
2020/04/10 PHP
ECMAScript 基础知识
2007/06/29 Javascript
JavaScript入门之基本函数详解
2011/10/21 Javascript
Bootstrap每天必学之前端开发框架
2015/11/19 Javascript
基于JS代码实现当鼠标悬停表格上显示这一格的全部内容
2016/06/12 Javascript
js实现上传文件添加和删除文件选择框
2016/10/24 Javascript
vue任意关系组件通信与跨组件监听状态vue-communication
2020/10/18 Javascript
centos下更新Python版本的步骤
2013/02/12 Python
Python中实现switch功能实例解析
2018/01/11 Python
Python实现从log日志中提取ip的方法【正则提取】
2018/03/31 Python
Python将图片转换为字符画的方法
2020/06/16 Python
Python爬虫常用小技巧之设置代理IP
2018/09/13 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
简单了解python协程的相关知识
2019/08/31 Python
基于Python生成个性二维码过程详解
2020/03/05 Python
用python写爬虫简单吗
2020/07/28 Python
如何基于Python爬虫爬取美团酒店信息
2020/11/03 Python
用python获取txt文件中关键字的数量
2020/12/24 Python
纯CSS实现预加载动画效果
2017/09/06 HTML / CSS
HTML中meta标签及Keywords
2020/04/15 HTML / CSS
俄罗斯购买内衣网站:Trusiki
2020/08/22 全球购物
线程的基本概念、线程的基本状态以及状态之间的关系
2012/10/26 面试题
热门专业求职信
2014/05/24 职场文书
校本课程教学计划
2015/01/19 职场文书
Nginx设置HTTPS的方法步骤 443证书配置方法
2022/03/21 Servers
分享Python异步爬取知乎热榜
2022/04/12 Python
Python使用plt.boxplot()函数绘制箱图、常用方法以及含义详解
2022/08/14 Python