php重寫分頁器CLinkPager的範例程式碼分享

黄舟
發布: 2023-03-16 20:40:01
原創
1058 人瀏覽過

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=&#39;first&#39;;
  const CSS_LAST_PAGE=&#39;last&#39;;
  const CSS_PREVIOUS_PAGE=&#39;previous&#39;;
  const CSS_NEXT_PAGE=&#39;next&#39;;
  const CSS_INTERNAL_PAGE=&#39;page&#39;;
  const CSS_HIDDEN_PAGE=&#39;hidden&#39;;
  const CSS_SELECTED_PAGE=&#39;selected&#39;;

  /**
   * @var string the CSS class for the first page button. Defaults to &#39;first&#39;.
   * @since 1.1.11
   */
  public $firstPageCssClass=self::CSS_FIRST_PAGE;
  /**
   * @var string the CSS class for the last page button. Defaults to &#39;last&#39;.
   * @since 1.1.11
   */
  public $lastPageCssClass=self::CSS_LAST_PAGE;
  /**
   * @var string the CSS class for the previous page button. Defaults to &#39;previous&#39;.
   * @since 1.1.11
   */
  public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
  /**
   * @var string the CSS class for the next page button. Defaults to &#39;next&#39;.
   * @since 1.1.11
   */
  public $nextPageCssClass=self::CSS_NEXT_PAGE;
  /**
   * @var string the CSS class for the internal page buttons. Defaults to &#39;page&#39;.
   * @since 1.1.11
   */
  public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
  /**
   * @var string the CSS class for the hidden page buttons. Defaults to &#39;hidden&#39;.
   * @since 1.1.11
   */
  public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
  /**
   * @var string the CSS class for the selected page buttons. Defaults to &#39;selected&#39;.
   * @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 &#39;Next >&#39;.
   */
  public $nextPageLabel;
  /**
   * @var string the text label for the previous page button. Defaults to &#39;< Previous&#39;.
   */
  public $prevPageLabel;
  /**
   * @var string the text label for the first page button. Defaults to &#39;<< First&#39;.
   */
  public $firstPageLabel;
  /**
   * @var string the text label for the last page button. Defaults to &#39;Last >>&#39;.
   */
  public $lastPageLabel;
  /**
   * @var string the text shown before page buttons. Defaults to &#39;Go to page: &#39;.
   */
  public $header;
  /**
   * @var string the text shown after page buttons.
   */
  public $footer=&#39;&#39;;
  /**
   * @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(&#39;yii&#39;,&#39;Next >&#39;);
    if($this->prevPageLabel===null)
      $this->prevPageLabel=Yii::t(&#39;yii&#39;,&#39;< Previous&#39;);
    //if($this->firstPageLabel===null)
    // $this->firstPageLabel=Yii::t(&#39;yii&#39;,&#39;<< First&#39;);
    //if($this->lastPageLabel===null)
    // $this->lastPageLabel=Yii::t(&#39;yii&#39;,&#39;Last >>&#39;);
    if($this->header===null)
      $this->header=Yii::t(&#39;yii&#39;,&#39;Go to page: &#39;);

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

  /**
   * 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(&#39;ul&#39;,$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=&#39;background:#a3a3a3&#39;><上一頁</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 == &#39;both&#39;){
      $buttons[] = "<span style=&#39;background:#a3a3a3&#39;>...</span>";
    }
    for($i=$beginPage;$i<=$endPage;++$i){
      if($ellipsis == &#39;left&#39; && $i == $beginPage){
        $buttons[] = "<span style=&#39;background:#a3a3a3&#39;>...</span>";
      }
      $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
      if($ellipsis == &#39;right&#39; && $i == $endPage){
        $buttons[] = "<span style=&#39;background:#a3a3a3&#39;>...</span>";
      }
    }  
    if($ellipsis == &#39;both&#39;){
      $buttons[] = "<span style=&#39;background:#a3a3a3&#39;>...</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=&#39;background:#a3a3a3&#39;>下一頁></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.=&#39; &#39;.($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(&#39;a&#39;);
        $beginPage = $currentPage - 2;
        $endPage = $currentPage + 2;
        $ellipsis = &#39;both&#39;;
      }else{
        $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
        if($beginPage == 1){
          $ellipsis = &#39;right&#39;;
        }else{
          $ellipsis = &#39;left&#39;;
        }
        if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
        {
          // print_r(&#39;b&#39;);
          $endPage=$pageCount-2;
          $beginPage=max(1,$endPage-$this->maxButtonCount+1);
        }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){
          // print_r(&#39;c&#39;);
          $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(&#39;system.web.widgets.pagers.pager&#39;).&#39;.css&#39;);
    Yii::app()->getClientScript()->registerCssFile($url);
  }
}
登入後複製

3、呼叫方式

在View裡的對應widget,定義pager的class為自訂的分頁器類別名稱即可,參考:

$this->widget(&#39;zii.widgets.CListView&#39;, array(
  &#39;dataProvider&#39;=>$dataProvider,
  &#39;itemView&#39;=>&#39;_view_t&#39;,
  &#39;pager&#39;=>array(
  &#39;class&#39;=>&#39;MyPager&#39;,
 )
));
登入後複製

以上是php重寫分頁器CLinkPager的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板