欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 重写分页器 CLinkPager的实例

程序员文章站 2022-03-10 21:09:02
php 重写分页器 clinkpager的实例 1、自定义的分页器类放在哪里? 有两个位置可以放, 第一种是放在 protected/extensions 中...

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',
 )
));

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