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 相关文章推荐
PHP4实际应用经验篇(8)
Oct 09 PHP
Linux下将excel数据导入到mssql数据库中的方法
Feb 08 PHP
《PHP编程最快明白》第六讲:Mysql数据库操作
Nov 01 PHP
WordPress判断用户是否登录的代码
Mar 17 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
Jul 03 PHP
php将mysql数据库整库导出生成sql文件的具体实现
Jan 08 PHP
详解PHP中的PDO类
Jul 06 PHP
php实现网站顶踩功能的完整前端代码
Jul 19 PHP
通过chrome浏览器控制台(Console)进行PHP Debug的方法
Oct 19 PHP
thinkPHP数据库增删改查操作方法实例详解
Dec 06 PHP
php中对象引用和复制实例分析
Aug 14 PHP
PHP如何解决微信文章图片防盗链
Dec 09 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遍历树的常用方法汇总
2015/06/18 PHP
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
判断用户是否在线的代码
2011/03/05 Javascript
基于jQuery实现的当离开页面时出现提示的实现代码
2011/06/27 Javascript
javascript如何动态加载表格与动态添加表格行
2013/11/27 Javascript
浅析jquery的作用与优势
2013/12/02 Javascript
15位和18位身份证JS校验的简单实例
2016/07/18 Javascript
bootstrap laydate日期组件使用详解
2017/01/04 Javascript
laravel5.4+vue+element简单搭建的示例代码
2017/08/29 Javascript
Vue中computed与methods的区别详解
2018/03/24 Javascript
深入理解Puppeteer的入门教程和实践
2019/03/05 Javascript
jquery选择器和属性对象的操作实例分析
2020/01/10 jQuery
js获取本日、本周、本月的时间代码
2020/02/01 Javascript
JavaScript indexOf()原理及使用方法详解
2020/07/09 Javascript
python负载均衡的简单实现方法
2018/02/04 Python
在Pycharm中执行scrapy命令的方法
2019/01/16 Python
python实现多层感知器
2019/01/18 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结
2019/07/04 Python
python爬虫之快速对js内容进行破解
2019/07/09 Python
django多种支付、并发订单处理实例代码
2019/12/13 Python
python实现数学模型(插值、拟合和微分方程)
2020/11/13 Python
python实现杨辉三角的几种方法代码实例
2021/03/02 Python
使用CSS3制作响应式导航菜单的方法
2015/07/12 HTML / CSS
纯CSS3实现漂亮的input输入框动画样式库(Text input love)
2018/12/29 HTML / CSS
Otel.com:折扣酒店预订
2017/08/24 全球购物
工商管理实习自我鉴定
2013/09/28 职场文书
护理专业本科生自荐信
2013/10/01 职场文书
办加油卡单位介绍信
2014/01/09 职场文书
信息科学与技术专业求职信范文
2014/02/20 职场文书
《故都的秋》教学反思
2014/04/15 职场文书
合法的离婚协议书范本
2014/10/23 职场文书
农村党建工作汇报材料
2014/10/27 职场文书
关于远足的感想
2015/08/10 职场文书
职场中的你,辞职信写对了吗?
2019/06/26 职场文书
Docker下安装Oracle19c
2022/04/13 Servers