Yii使用CLinkPager分页实例详解

落墨大大_5648

落墨大大_5648

2016-12-23

1403人浏览

原创

本文主要讲解了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())getPageRange();
 $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
 $buttons=array();
  
 // first page
 $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPagecreatePageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPagecreatePageButton($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 '
  • '.CHtml::label($label,false).'
  • ';  }     /**  * 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使用CLinkPager分页实例详解相关文章请关注PHP中文网!

    PHP速学视频免费教程(入门到精通)
    PHP速学视频免费教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载

    相关标签:

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    相关专题

    更多
    GoFrame框架Web开发与路由中间件
    GoFrame框架Web开发与路由中间件

    本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

    2026.11.17

    0

    15

    GoFrame框架安装与gf命令行工具
    GoFrame框架安装与gf命令行工具

    本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

    2026.11.17

    0

    15

    GoFrame框架零基础入门
    GoFrame框架零基础入门

    本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

    2026.11.17

    0

    15

    SQLAlchemy 一对多关系怎么建立
    SQLAlchemy 一对多关系怎么建立

    SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

    2026.11.17

    0

    10

    SQLAlchemy 如何增删改查
    SQLAlchemy 如何增删改查

    SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

    2026.11.17

    0

    11

    SQLAlchemy ORM 模型怎么写
    SQLAlchemy ORM 模型怎么写

    SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

    2026.11.17

    0

    16

    Go-Zero API开发使用教程
    Go-Zero API开发使用教程

    本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

    2026.11.12

    160

    15

    Go-Zero安装与goctl环境配置教程
    Go-Zero安装与goctl环境配置教程

    本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

    2026.11.12

    120

    15

    Go-Zero零基础入门教程
    Go-Zero零基础入门教程

    本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

    2026.11.12

    100

    15

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    热门推荐
    /
    最新课程
    phpStudy极速入门视频教程
    phpStudy极速入门视频教程

    共6课时 | 54.7万人学习

    独孤九贱(4)_PHP视频教程
    独孤九贱(4)_PHP视频教程

    共89课时 | 134.3万人学习