Yii使用CLinkPager分页实例详解

   2015-09-17 0
核心提示:这篇文章主要介绍了Yii使用CLinkPager分页的方法,需要的朋友可以参考下

本文主要讲解了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,));
 >
 
标签: Yii 分页
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Yii 框架学习:如何复制一个Ruby on Rails。
    Yii 框架学习:如何复制一个Ruby on Rails。
     早上在ruby-china发了个帖子,询问“现今PHP的框架中最接近RAILS的是什么?”大部分答曰"Yii”。所以晚上回来就来学下这个框架看看。 Yii 是一个基于组件、纯OOP的、用于开发大型 Web 应用的高性能 PHP 框架。 下载回来的软件包解压后结构如下:CHANGELOG
    02-09
  • yii 隐藏index.php
        首先,开启apache的rewrite模块    去掉rewrite前的#,如下所示    LoadModule rewrite_module modules/mod_rewrite.so     接着,在yii的index.php同级目录建立.htaccess文件,内容如下    IfModule mod_rewrite.c       Optio
    02-09
  • php Yii2框架创建定时任务方法详解
    目录简单的定时任务脚步文件传递参数设置参数别名在Linux配置crontab在我们项目开发中,需要使用定时任务来完成批处理业务,下面通过Yii2框架来实现定时任务Yii2框架定时任务代码统一放在 ./console 目录下简单的定时任务脚步文件首先在console/controller目
  • yii2的数据库层设计
    Yii2在M层的抽象做的很经典,值得分析理解。QueryBuilder不同的数据库引擎(mysql,oracle…)执行相同的SQL,可能语法有一些细微的差异,但是SQL整体语法差别不大,所以会定义一个QueryBuilder基类,它提供一般通用的SQL生成方法,具体每个数据库引擎继承Quer
    02-05 SQLYii
  • 码云推荐 | 基于Yii2的后台开源骨架 openadm-yi
    OpenAdmOpenAdm是一个基于Yii2的后台开源骨架,集成了用户和插件系统,使用主题功能,默认使用AdminLTE2的模板的主题,可以非常方便的开发新的功能。Demo地址:http://yii2.openadm.com后台账号: admin后台密码: admin仓库地址:OSC:https://git.oschina.net/xiong
    02-05 Yii开源
  • Yii2配置多语言
    我的YII2版本是2.0.7, 设置多语言时和其他教程有不同的地方, 所以整理如下1. 在一个controller里面写一个调用i18n的语句, 比如actionIndexecho \Yii::t('app', 'whatisthis');现在进入这个页面, 页面输出whatisthis2. 手动创建一个文件夹messages来存放多语言
    02-05 Yii
  • Yii2 的问题解决方案
    Yii2 的问题解决方案
    yii2 Class 'app\controllers\AccessControl' not found一般是命名空间问题, 写成\yii\filters\AccessControl::className(),yii2想要新增第三方vendor, 修改composer.json,require-dev: {yiisoft/yii2-codeception: *,yiisoft/yii2-debug: *,yiisoft/yii2-gii
    02-05 Yii
  • Yii 2.0.11.1 发布,高性能 PHP 框架
    Yii 2.0.11.1 发布了。Yii是一个高性能的PHP5的web应用程序开发框架。通过一个简单的命令行工具 yiic 可以快速创建一个web应用程序的代码框架,开发者可以在生成的代码框架基础上添加业务逻辑,以快速完成应用程序的开发。 更新内容: Bug #11502: Fixed yii\
    02-05 Yii高性能
  • 码云推荐 | 基于 yii2 的快速配置 api 服务 yii2-fast-api
    码云推荐 | 基于 yii2 的快速配置 api 服务 yii
    yii2-fast-api yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发。此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用restfull的标准,方便前端开发处理接口数据以及各种异常。Installation 使用 Composer 安
    02-05 YiiAPI
  • 简述Yii2里的AOP思想
    AOP是什么在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP解决什么将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过
    12-23 AOPYii
点击排行