php 重写分页器 CLinkPager的实例

php 重写分页器 CLinkPager的实例

1、自定义的分页器类放在哪里?

有两个位置可以放,

第一种是放在 protected/extensions 中,在使用是import进来,或在config文件中import进来;

第二种是放在 protected/components 中,作为组件存在,不需要import

2、用派生方式是最好的

class MyPager extends CLinkPager 

入口函数是:public function run() ,当显示分页器时run()被调用,里面的输出就会显示在相应位置;

其他的完全自定义,如果你不知道上一页、下一页、首页、尾页、总页数、当前页码等信息,可以参考CLinkPager的源码,yii/frameworks/web/widgets/pagers/CLinkPager.php

<?php

class MyPager extends CLinkPager
{
  const CSS_FIRST_PAGE='first';
  const CSS_LAST_PAGE='last';
  const CSS_PREVIOUS_PAGE='previous';
  const CSS_NEXT_PAGE='next';
  const CSS_INTERNAL_PAGE='page';
  const CSS_HIDDEN_PAGE='hidden';
  const CSS_SELECTED_PAGE='selected';

  /**
   * @var string the CSS class for the first page button. Defaults to 'first'.
   * @since 1.1.11
   */
  public $firstPageCssClass=self::CSS_FIRST_PAGE;
  /**
   * @var string the CSS class for the last page button. Defaults to 'last'.
   * @since 1.1.11
   */
  public $lastPageCssClass=self::CSS_LAST_PAGE;
  /**
   * @var string the CSS class for the previous page button. Defaults to 'previous'.
   * @since 1.1.11
   */
  public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
  /**
   * @var string the CSS class for the next page button. Defaults to 'next'.
   * @since 1.1.11
   */
  public $nextPageCssClass=self::CSS_NEXT_PAGE;
  /**
   * @var string the CSS class for the internal page buttons. Defaults to 'page'.
   * @since 1.1.11
   */
  public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
  /**
   * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'.
   * @since 1.1.11
   */
  public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
  /**
   * @var string the CSS class for the selected page buttons. Defaults to 'selected'.
   * @since 1.1.11
   */
  public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
  /**
   * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
   */
  public $maxButtonCount=10;
  /**
   * @var string the text label for the next page button. Defaults to 'Next >'.
   */
  public $nextPageLabel;
  /**
   * @var string the text label for the previous page button. Defaults to '< Previous'.
   */
  public $prevPageLabel;
  /**
   * @var string the text label for the first page button. Defaults to '<< First'.
   */
  public $firstPageLabel;
  /**
   * @var string the text label for the last page button. Defaults to 'Last >>'.
   */
  public $lastPageLabel;
  /**
   * @var string the text shown before page buttons. Defaults to 'Go to page: '.
   */
  public $header;
  /**
   * @var string the text shown after page buttons.
   */
  public $footer='';
  /**
   * @var mixed the CSS file used for the widget. Defaults to null, meaning
   * using the default CSS file included together with the widget.
   * If false, no CSS file will be used. Otherwise, the specified CSS file
   * will be included when using this widget.
   */
  public $cssFile;
  /**
   * @var array HTML attributes for the pager container tag.
   */
  public $htmlOptions=array();

  /**
   * Initializes the pager by setting some default property values.
   */
  public function init()
  {
    if($this->nextPageLabel===null)
      $this->nextPageLabel=Yii::t('yii','Next >');
    if($this->prevPageLabel===null)
      $this->prevPageLabel=Yii::t('yii','< Previous');
    //if($this->firstPageLabel===null)
    // $this->firstPageLabel=Yii::t('yii','<< First');
    //if($this->lastPageLabel===null)
    // $this->lastPageLabel=Yii::t('yii','Last >>');
    if($this->header===null)
      $this->header=Yii::t('yii','Go to page: ');

    if(!isset($this->htmlOptions['id']))
      $this->htmlOptions['id']=$this->getId();
    if(!isset($this->htmlOptions['class']))
      $this->htmlOptions['class']='yiiPager';
  }

  /**
   * Executes the widget.
   * This overrides the parent implementation by displaying the generated page buttons.
   */
  public function run()
  {
    $this->registerClientScript();
    $buttons=$this->createPageButtons();
    if(empty($buttons))
      return;
    echo $this->header;
//   echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
    echo implode("\n",$buttons);
    echo $this->footer;
  }

  /**
   * Creates the page buttons.
   * @return array a list of page buttons (in HTML code).
   */
  protected function createPageButtons()
  {
    if(($pageCount=$this->getPageCount())<=1)
      return array();

    list($beginPage,$endPage,$ellipsis)=$this->getPageRange();

    $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
    $buttons=array();

    // first page
    //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);

    // prev page
    if(($page=$currentPage-1)<0)
      $page=0;
    if($currentPage == 0){
      $buttons[] = "<span style='background:#a3a3a3'><上一頁</span>";
    }else{
      $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
    }
    // internal pages start
    // first
    $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage);
    //middle
    if($ellipsis == 'both'){
      $buttons[] = "<span style='background:#a3a3a3'>...</span>";
    }
    for($i=$beginPage;$i<=$endPage;++$i){
      if($ellipsis == 'left' && $i == $beginPage){
        $buttons[] = "<span style='background:#a3a3a3'>...</span>";
      }
      $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
      if($ellipsis == 'right' && $i == $endPage){
        $buttons[] = "<span style='background:#a3a3a3'>...</span>";
      }
    }
    if($ellipsis == 'both'){
      $buttons[] = "<span style='background:#a3a3a3'>...</span>";
    }
    // last
    $buttons[]=$this->createPageButton($pageCount,$pageCount - 1,$this->internalPageCssClass,false,$i==$currentPage);
    // internal pages end
    // next page
    if(($page=$currentPage+1)>=$pageCount-1)
      $page=$pageCount-1;
    if($currentPage == ($pageCount-1)){
      $buttons[] = "<span style='background:#a3a3a3'>下一頁></span>";
    }else{
      $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
    }
    // last page
    //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);

    return $buttons;
  }

  /**
   * Creates a page button.
   * You may override this method to customize the page buttons.
   * @param string $label the text label for the button
   * @param integer $page the page number
   * @param string $class the CSS class for the page button.
   * @param boolean $hidden whether this page button is visible
   * @param boolean $selected whether this page button is selected
   * @return string the generated button
   */
  protected function createPageButton($label,$page,$class,$hidden,$selected)
  {
    if($hidden || $selected)
      $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
    if ($selected) {
      $result = "<span>" . ++$page . "</span>";
    } else {
      $result = CHtml::link($label,$this->createPageUrl($page));
    }
    return $result;
  }

  /**
   * @return array the begin and end pages that need to be displayed.
   */
  protected function getPageRange()
  {
    $currentPage=$this->getCurrentPage();
    $pageCount=$this->getPageCount();
    /*$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
    if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
    {
      $endPage=$pageCount-1;
      $beginPage=max(0,$endPage-$this->maxButtonCount+1);
    }*/
    if($pageCount > $this->maxButtonCount){
      if($currentPage > 4 && $currentPage < ($pageCount - 4)){
        // print_r('a');
        $beginPage = $currentPage - 2;
        $endPage = $currentPage + 2;
        $ellipsis = 'both';
      }else{
        $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
        if($beginPage == 1){
          $ellipsis = 'right';
        }else{
          $ellipsis = 'left';
        }
        if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
        {
          // print_r('b');
          $endPage=$pageCount-2;
          $beginPage=max(1,$endPage-$this->maxButtonCount+1);
        }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){
          // print_r('c');
          $endPage=$pageCount-2;
        }

      }
    }else{
      $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
      if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
      {
        $endPage=$pageCount-2;
        $beginPage=max(1,$endPage-$this->maxButtonCount+1);
      }
    }

    return array($beginPage,$endPage, $ellipsis);
  }

  /**
   * 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('system.web.widgets.pagers.pager').'.css');
    Yii::app()->getClientScript()->registerCssFile($url);
  }
}

3、调用方式

在View里的相应widget,定义pager的class为自定义的分页器类名即可,参考:

$this->widget('zii.widgets.CListView', array(
  'dataProvider'=>$dataProvider,
  'itemView'=>'_view_t',
  'pager'=>array(
  'class'=>'MyPager',
 )
));

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(0)

相关推荐

  • thinkPHP实现的验证码登录功能示例

    本文实例讲述了thinkPHP实现的验证码登录功能.分享给大家供大家参考,具体如下: 使用thinkphp自带的验证,实现登录页面的账号密码+验证码的验证 <?php namespace Admin\Controller; use Think\Controller; use Think\Verify; class LoginController extends Controller{ public function login(){ if($_POST){ $obj = new Verify()

  • 史上最全的PHP正则表达式(手机号需要加上177-***)

    首先看下正则表达式思维导图: 一.校验数字的表达式  1 数字: ^[0-9]*$ 2 n位的数字: ^\d{n}$ 3 至少n位的数字: ^\d{n,}$ 4 m-n位的数字: ^\d{m,n}$ 5 零和非零开头的数字: ^(0|[1-9][0-9]*)$ 6 非零开头的最多带两位小数的数字: ^([1-9][0-9]*)+(.[0-9]{1,2})?$ 7 带1-2位小数的正数或负数: ^(\-)?\d+(\.\d{1,2})?$ 8 正数.负数.和小数: ^(\-|\+)?\d+(\.\

  • PHP实现找出数组中出现次数超过数组长度一半的数字算法示例

    本文实例讲述了PHP实现找出数组中出现次数超过数组长度一半的数字算法.分享给大家供大家参考,具体如下: <?php * 算法要求:数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字. * * 算法分析:我们需要计算数组中每个数字的出现次数.在PHP中我们可以使用in_array函数 * 来判断一个元素是否出现在数组中.比如数组中含有1,2,3三个元素,我们要判断1是否存在 * 可以使用in_array(1,$array)来判断,但是这样只能判断1出现了一次,因为对于含有数组 * 元素1

  • PHP实现网站访问量计数器

    简单的网站访问量计数器实现,具体如下 首先说明思路: 1.用户向服务器发出访问请求 2.服务器读取访问次数文件,+1,向客户端返回 3.服务器保存新的浏览次数 4.新用户访问,重复123即可 解决方案(主要算法): 1.数据文件:counter.dat 2.读出数据文件     打开文件:     如果不存在,创建,并以0为初识数据:     否则,读出数据:     关闭文件. 3.把累加后的数据写入文件counter.dat     累加数据:     打开文件:     写入数据:    

  • php+ajax+h5实现图片上传功能

    本文实例为大家分享了php实现ajax图片上传的具体代码,供大家参考,具体内容如下 html页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="__PUBLIC__

  • 利用php获得flv视频长度的实例代码

    废话不多说了,直接给大家贴代码了,具体代码如下所示: function BigEndian2Int($byte_word, $signed = false) { $int_value = 0; $byte_wordlen = strlen($byte_word); for ($i = 0; $i < $byte_wordlen; $i++) { $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i)); } if

  • PHP用函数嵌入网站访问量计数器

    这是一种方法实现计数器.想看另一种方法的请点击:[PHP]简单的网站访问量计数器实现 想看具体代码思路的也请点击上面的链接. 创建Embed-Count文件夹 在Embed-Count文件夹下面创建counter.inc.php文件,内容如下: <?php function counter(){ $counter = 0; //初始化变量 $max_len = 8; $lj = explode("/",$_SERVER["PHP_SELF"]); //超全局变

  • thinkPHP实现上传图片及生成缩略图功能示例

    本文实例讲述了thinkPHP实现上传图片及生成缩略图功能.分享给大家供大家参考,具体如下: 记录一下在thinkPHP上传图片的方法(Upload)和生成缩略图(Image)的方法. html页面form中必须加enctype="multipart/form-data" <form action="__SELF__" method="post" enctype="multipart/form-data"> <

  • php 重写分页器 CLinkPager的实例

    php 重写分页器 CLinkPager的实例 1.自定义的分页器类放在哪里? 有两个位置可以放, 第一种是放在 protected/extensions 中,在使用是import进来,或在config文件中import进来: 第二种是放在 protected/components 中,作为组件存在,不需要import 2.用派生方式是最好的 class MyPager extends CLinkPager 入口函数是:public function run() ,当显示分页器时run()被调用

  • Java 重载、重写、构造函数的实例详解

    Java 重载.重写.构造函数的实例详解 方法重写 1.重写只能出现在继承关系之中.当一个类继承它的父类方法时,都有机会重写该父类的方法.一个特例是父类的方法被标识为final.重写的主要优点是能够定义某个子类型特有的行为. class Animal { public void eat(){ System.out.println ("Animal is eating."); } } class Horse extends Animal{ public void eat(){ Syste

  • js简单的分页器插件代码实例

    这篇文章主要介绍了js简单的分页器插件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 自己引入jquery插件,我的demo是引入的自己本地的query 代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/

  • Vue引用Swiper4插件无法重写分页器样式的解决方法

    最近在尝试用nuxtjs来搭建新的站点,但是平时在jquery里面用惯的一些插件一到vue上面引用就各种不顺畅~本文记录一下在用Swiper插件来做轮播图的时候遇到的问题~至于怎么在vue里面引用插件就不累赘了,npm能告诉你~ Swiper的分页器是靠mounted()挂载到Vue组件上而不是直接写在template里,所以在style scoped中写的样式无法作用到分页器的点上.解决办法是把重写的样式写在scoped之外.(以下截图不完整,仅用做说明) template: script:

  • java方法重写和super关键字实例详解

     java方法重写和super关键字  在继承中,其实就是子类定义了和父类同名的方法 就是方法,属性都是相通的 重写限制:  被子类重写的方法不能拥有比父类方法更加严格的权限 super:强行调用父类方法的执行 重载和重写的区别?  重 载是发生在一个类中   对权限没有要求   而且重载的方法参数可以不同 重写发生在继承汇总      被子类重写的方法不能拥有比父类方法更加严格的权限,重写的方法中参数名字完全相同  实例代码: class A{ public void tell(){ Syst

  • nginx重写rewrite基础及实例分享

    nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否存在文件 -d和!-d用来判断是否存在目录 -e和!-e用来判断是否存在文件或目录 -x和!-x用来判断文件是否可执行 flag标记 last 相当于Apache里的[L]标记,表示完成rewrite break 终止匹配, 不再匹配后面的规则. redirect 返回302临时重定向 地址栏会显示

  • Android重写View并自定义属性实例分析

    本文实例分析了Android重写View并自定义属性的方法.分享给大家供大家参考,具体如下: 这里通过自定义属性 实现如下图所示效果: 第一步:在res\values的目录下新建一个文件attrs.xml 声明一些自定义属性 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomViewStyle">

  • 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.yiifra

  • javascript重写alert方法的实例代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="

  • aspnetpager重写url(伪静态)配置实例

    复制代码 代码如下: <webdiyer:AspNetPager UrlPaging="true" EnableUrlRewriting="true"  UrlRewritePattern="/bidding/list_%tid%_%cityid%_{0}_%isProvince%.aspx"  ID="pager" CssClass="page" runat="server" O

随机推荐