php 重写分页器 CLinkPager的实例

PHP 重写分页器 CLinkPager的实例

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

有两个位置可以放,

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

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

2、用派生方式是最好的

rush:PHP;"> class MyPager extends CLinkPager

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

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

rush: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 cal<a href="https://www.jb51.cc/tag/cula/" target="_blank" class="keywords">cula</a>ted in getPageRange()
$buttons=array();

// f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>t page
//$buttons[]=$this->createPageButton($this->f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tPageLabel,$this->f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tPageCssClass,$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->prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>sPageCssClass,false);
}
// internal pages start
// f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>t
$buttons[]=$this->createPageButton(1,$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,$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,$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,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
}
// last page
//$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,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,$class,$hidden,$selected)
    {
    if($hidden || $selected)
    $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
    if ($selected) {
    $result = "" . ++$page . "";
    } 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,$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为自定义分页器类名即可,参考:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用