当前位置:首页 > PHP教程 > php分页 > 列表

php 重写分页器 CLinkPager的实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-17 10:38:04 浏览: 评论:0 

这篇文章主要介绍了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

  1. <?php 
  2.  
  3. class MyPager extends CLinkPager 
  4. { 
  5.   const CSS_FIRST_PAGE='first'; 
  6.   const CSS_LAST_PAGE='last'; 
  7.   const CSS_PREVIOUS_PAGE='previous'; 
  8.   const CSS_NEXT_PAGE='next'; 
  9.   const CSS_INTERNAL_PAGE='page'; 
  10.   const CSS_HIDDEN_PAGE='hidden'; 
  11.   const CSS_SELECTED_PAGE='selected'; 
  12.  
  13.   /** 
  14.    * @var string the CSS class for the first page button. Defaults to 'first'. 
  15.    * @since 1.1.11 
  16.    */ 
  17.   public $firstPageCssClass=self::CSS_FIRST_PAGE; 
  18.   /** 
  19.    * @var string the CSS class for the last page button. Defaults to 'last'. 
  20.    * @since 1.1.11 
  21.    */ 
  22.   public $lastPageCssClass=self::CSS_LAST_PAGE; 
  23.   /** 
  24.    * @var string the CSS class for the previous page button. Defaults to 'previous'. 
  25.    * @since 1.1.11 
  26.    */ 
  27.   public $previousPageCssClass=self::CSS_PREVIOUS_PAGE; 
  28.   /** 
  29.    * @var string the CSS class for the next page button. Defaults to 'next'. 
  30.    * @since 1.1.11 
  31.    */ 
  32.   public $nextPageCssClass=self::CSS_NEXT_PAGE; 
  33.   /** 
  34.    * @var string the CSS class for the internal page buttons. Defaults to 'page'. 
  35.    * @since 1.1.11 
  36.    */ 
  37.   public $internalPageCssClass=self::CSS_INTERNAL_PAGE; 
  38.   /** 
  39.    * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'. 
  40.    * @since 1.1.11 
  41.    */ 
  42.   public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE; 
  43.   /** 
  44.    * @var string the CSS class for the selected page buttons. Defaults to 'selected'. 
  45.    * @since 1.1.11 
  46.    */ 
  47.   public $selectedPageCssClass=self::CSS_SELECTED_PAGE; 
  48.   /** 
  49.    * @var integer maximum number of page buttons that can be displayed. Defaults to 10. 
  50.    */ 
  51.   public $maxButtonCount=10; 
  52.   /** 
  53.    * @var string the text label for the next page button. Defaults to 'Next >'. 
  54.    */ 
  55.   public $nextPageLabel; 
  56.   /** 
  57.    * @var string the text label for the previous page button. Defaults to '< Previous'. 
  58.    */ 
  59.   public $prevPageLabel; 
  60.   /** 
  61.    * @var string the text label for the first page button. Defaults to '<< First'. 
  62.    */ 
  63.   public $firstPageLabel; 
  64.   /** 
  65.    * @var string the text label for the last page button. Defaults to 'Last >>'. 
  66.    */ 
  67.   public $lastPageLabel; 
  68.   /** 
  69.    * @var string the text shown before page buttons. Defaults to 'Go to page: '. 
  70.    */ 
  71.   public $header; 
  72.   /** 
  73.    * @var string the text shown after page buttons. 
  74.    */ 
  75.   public $footer=''; 
  76.   /** 
  77.    * @var mixed the CSS file used for the widget. Defaults to null, meaning 
  78.    * using the default CSS file included together with the widget. 
  79.    * If false, no CSS file will be used. Otherwise, the specified CSS file 
  80.    * will be included when using this widget. 
  81.    */ 
  82.   public $cssFile; 
  83.   /** 
  84.    * @var array HTML attributes for the pager container tag. 
  85.    */ 
  86.   public $htmlOptions=array(); 
  87.  
  88.   /** 
  89.    * Initializes the pager by setting some default property values. 
  90.    */ 
  91.   public function init() 
  92.   { 
  93.     if($this->nextPageLabel===null) 
  94.       $this->nextPageLabel=Yii::t('yii','Next >'); 
  95.     if($this->prevPageLabel===null) 
  96.       $this->prevPageLabel=Yii::t('yii','< Previous'); 
  97.     //if($this->firstPageLabel===null) 
  98.     // $this->firstPageLabel=Yii::t('yii','<< First'); 
  99.     //if($this->lastPageLabel===null) 
  100.     // $this->lastPageLabel=Yii::t('yii','Last >>'); 
  101.     if($this->header===null) 
  102.       $this->header=Yii::t('yii','Go to page: '); 
  103.  
  104.     if(!isset($this->htmlOptions['id'])) 
  105.       $this->htmlOptions['id']=$this->getId(); 
  106.     if(!isset($this->htmlOptions['class'])) 
  107.       $this->htmlOptions['class']='yiiPager'; 
  108.   } 
  109.  
  110.   /** 
  111.    * Executes the widget. 
  112.    * This overrides the parent implementation by displaying the generated page buttons. 
  113.    */ 
  114.   public function run() 
  115.   { 
  116.     $this->registerClientScript(); 
  117.     $buttons=$this->createPageButtons(); 
  118.     if(emptyempty($buttons)) 
  119.       return; 
  120.     echo $this->header; 
  121. //   echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons)); 
  122.     echo implode("\n",$buttons); 
  123.     echo $this->footer; 
  124.   } 
  125.  
  126.   /** 
  127.    * Creates the page buttons. 
  128.    * @return array a list of page buttons (in HTML code). 
  129.    */ 
  130.   protected function createPageButtons() 
  131.   { 
  132.     if(($pageCount=$this->getPageCount())<=1) 
  133.       return array(); 
  134.  
  135.     list($beginPage,$endPage,$ellipsis)=$this->getPageRange(); 
  136.  
  137.     $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange() 
  138.     $buttons=array(); 
  139.  
  140.     // first page 
  141.     //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false); 
  142.  
  143.     // prev page 
  144.     if(($page=$currentPage-1)<0) 
  145.       $page=0; 
  146.     if($currentPage == 0){ 
  147.       $buttons[] = "<span style='background:#a3a3a3'><上一頁</span>"; 
  148.     }else{ 
  149.       $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false); 
  150.     } 
  151.     // internal pages start 
  152.     // first 
  153.     $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage); 
  154.     //middle 
  155.     if($ellipsis == 'both'){ 
  156.       $buttons[] = "<span style='background:#a3a3a3'>...</span>"; 
  157.     } 
  158.     for($i=$beginPage;$i<=$endPage;++$i){ 
  159.       if($ellipsis == 'left' && $i == $beginPage){ 
  160.         $buttons[] = "<span style='background:#a3a3a3'>...</span>"; 
  161.       } 
  162.       $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage); 
  163.       if($ellipsis == 'right' && $i == $endPage){ 
  164.         $buttons[] = "<span style='background:#a3a3a3'>...</span>"; 
  165.       } 
  166.     }   
  167.     if($ellipsis == 'both'){ 
  168.       $buttons[] = "<span style='background:#a3a3a3'>...</span>"; 
  169.     } 
  170.     // last 
  171.     $buttons[]=$this->createPageButton($pageCount,$pageCount - 1,$this->internalPageCssClass,false,$i==$currentPage); 
  172.     // internal pages end 
  173.     // next page 
  174.     if(($page=$currentPage+1)>=$pageCount-1) 
  175.       $page=$pageCount-1; 
  176.     if($currentPage == ($pageCount-1)){ 
  177.       $buttons[] = "<span style='background:#a3a3a3'>下一頁></span>"; 
  178.     }else{ 
  179.       $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false); 
  180.     } 
  181.     // last page 
  182.     //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false); 
  183.  
  184.     return $buttons; 
  185.   } 
  186.  
  187.   /** 
  188.    * Creates a page button. 
  189.    * You may override this method to customize the page buttons. 
  190.    * @param string $label the text label for the button 
  191.    * @param integer $page the page number 
  192.    * @param string $class the CSS class for the page button. 
  193.    * @param boolean $hidden whether this page button is visible 
  194.    * @param boolean $selected whether this page button is selected 
  195.    * @return string the generated button 
  196.    */ 
  197.   protected function createPageButton($label,$page,$class,$hidden,$selected) 
  198.   { 
  199.     if($hidden || $selected) 
  200.       $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass); 
  201.     if ($selected) { 
  202.       $result = "<span>" . ++$page . "</span>"; 
  203.     } else { 
  204.       $result = CHtml::link($label,$this->createPageUrl($page)); 
  205.     } 
  206.     return $result; 
  207.   } 
  208.  
  209.   /** 
  210.    * @return array the begin and end pages that need to be displayed. 
  211.    */ 
  212.   protected function getPageRange() 
  213.   { 
  214.     $currentPage=$this->getCurrentPage(); 
  215.     $pageCount=$this->getPageCount(); 
  216.     /*$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2)); 
  217.     if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) 
  218.     { 
  219.       $endPage=$pageCount-1; 
  220.       $beginPage=max(0,$endPage-$this->maxButtonCount+1); 
  221.     }*/ 
  222.     if($pageCount > $this->maxButtonCount){ 
  223.       if($currentPage > 4 && $currentPage < ($pageCount - 4)){ 
  224.         // print_r('a'); 
  225.         $beginPage = $currentPage - 2; 
  226.         $endPage = $currentPage + 2; 
  227.         $ellipsis = 'both'; 
  228.       }else{ 
  229.         $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2)); 
  230.         if($beginPage == 1){ 
  231.           $ellipsis = 'right'; 
  232.         }else{ 
  233.           $ellipsis = 'left'; 
  234.         } 
  235.         if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) 
  236.         { 
  237.           // print_r('b'); 
  238.           $endPage=$pageCount-2; 
  239.           $beginPage=max(1,$endPage-$this->maxButtonCount+1); 
  240.         }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){ 
  241.           // print_r('c'); 
  242.           $endPage=$pageCount-2; 
  243.         } 
  244.  
  245.       } 
  246.     }else{ 
  247.       $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2)); 
  248.       if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) 
  249.       { 
  250.         $endPage=$pageCount-2; 
  251.         $beginPage=max(1,$endPage-$this->maxButtonCount+1); 
  252.       } 
  253.     } 
  254.  
  255.     return array($beginPage,$endPage, $ellipsis); 
  256.   } 
  257.  
  258.   /** 
  259.    * Registers the needed client scripts (mainly CSS file). 
  260.    */ 
  261.   public function registerClientScript() 
  262.   { 
  263.     if($this->cssFile!==false) 
  264.       self::registerCssFile($this->cssFile); 
  265.   } 
  266.  
  267.   /** 
  268.    * Registers the needed CSS file. 
  269.    * @param string $url the CSS URL. If null, a default CSS URL will be used. 
  270.    */ 
  271.   public static function registerCssFile($url=null) 
  272.   { 
  273.     if($url===null) 
  274.       $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css'); 
  275.     Yii::app()->getClientScript()->registerCssFile($url); 
  276.   } 
  277. } 

3、调用方式

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

  1. $this->widget('zii.widgets.CListView', array( 
  2.   'dataProvider'=>$dataProvider, 
  3.   'itemView'=>'_view_t', 
  4.   'pager'=>array( 
  5.   'class'=>'MyPager', 
  6.  ) 
  7. ));

Tags: php分页器 CLinkPager

分享到: