副标题[/!--empirenews.page--]
php 重写分页器 CLinkPager的实例
1、自定义的分页器类放在哪里?
有两个位置可以放,
第一种是放在 protected/extensions 中,在使用是import进来,或在config文件中import进来;
第二种是放在 protected/components 中,作为组件存在,不需要import
2、用派生方式是最好的
入口函数是:public function run() ,当显示分页器时run()被调用,里面的输出就会显示在相应位置;
其他的完全自定义,如果你不知道上一页、下一页、首页、尾页、总页数、当前页码等信息,可以参考CLinkPager的源码,yii/frameworks/web/widgets/pagers/CLinkPager.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,$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,false);
}
// internal pages start
// first
$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;
}
(编辑:南通站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|