PHP Rewrite page Clinkpager sample code sharing

Source: Internet
Author: User
PHP Rewrite Clinkpager instance of the paging device

1. Where are the custom pager classes placed?

There are two places to put,

The first kind is put in the protected/extensions, in the use is import come in, or in config file import come in;

The second is placed in the protected/components, as components exist, do not need import

2, the best way to derive

Class Mypager extends Clinkpager

The entry function is: public functions run (), and when the pager is displayed, run () is called, and the output is displayed in the corresponding position;

Other fully customizable, if you do not know the previous page, next page, home, last, total pages, current page numbers and other information, you can refer to Clinkpager source code, yii/frameworks/web/widgets/pagers/clinkpager.php

<?phpclass 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 would be used.   Otherwise, the specified CSS file * is included when using the 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 ' >< prev </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 ' >...</sp      An> ";    }} 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 ' > Next ></span>"; }else{$buttons []= $this->createpagebutton ($this->nextpagelabel, $page, $this->nextpagecssclass,$    currentpage>= $pageCount -1,false); }//last page//$buttons []= $this-&Gt;createpagebutton ($this->lastpagelabel, $pageCount-1, $this->lastpagecssclass, $currentPage >=$    Pagecount-1,false);  return $buttons;   }/** * Creates a page button.   * You could 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 are visible * @param boolean $selected Whether this page button is Sele CTED * @return String The generated button */protected function Createpagebutton ($label, $page, $class, $hidden, $select ed) {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->cssfil  e);   }/** * Registers the needed CSS file. * @param string $url the CSS URL.   If NULL, a default CSS URL would be used. */public static function Registercssfile ($url =null) {if ($url ===null) $url =chtml::asset (Yii::getpathofalias (' sy Stem.web.widgets.pagers.pager '). css);  Yii::app ()->getclientscript ()->registercssfile ($url); }}

3. Call mode

In the corresponding widget in view, define pager's class as a custom pager class name, and refer to:

$this->widget (' Zii.widgets.CListView ', Array (  ' dataprovider ' and ' = ' $dataProvider,  ' itemview ' = ') _ view_t ',  ' pager ' =>array (  ' class ' = ' Mypager ',));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.