Hello,
Thanks to this post I’ve managed to customize my pager.
What I wanted is to display first and last links and to put the page number also on the first page, that by default it leaves it do nothing, I mean, if you want to go to the first page the url is controller/list, but I wanted it to be controller/list/page/1.
So this is what I’ve done:
Created two extensions for CLinkPager and for CPagination and I have made a copy of the pager.css for myself.
Copy framework/web/widgets/pagers/pager.css to your folder for css files, and then change the following that hides the first and last links.
/**
* Hide first and last buttons by default.
*/
ul.yiiPager .first,
ul.yiiPager .last
{
/* display:none; */
}
MyLinkPager
<?php
class MyLinkPager extends CLinkPager {
// Add my custom pager css where I remove the hidden property from the first and the last item.
public function __construct() {
$this->cssFile = Yii::app()->request->baseUrl.'/css/pager.css';
}
/**
* 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)=$this->getPageRange();
$currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
$buttons=array();
// first page
$buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,false,false);
// prev page
if(($page=$currentPage-1)<0)
$page=0;
$buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);
// internal pages
for($i=$beginPage;$i<=$endPage;++$i)
$buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);
// next page
if(($page=$currentPage+1)>=$pageCount-1)
$page=$pageCount-1;
$buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);
// last page
$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$endPage>=$pageCount-1,false);
return $buttons;
}
/**
* Creates the default pagination.
* This is called by {@link getPages} when the pagination is not set before.
* @return CPagination the default pagination instance.
*/
protected function createPages()
{
return new MyPagination;
}
}
?>
MyPagination
<?php
class MyPagination extends CPagination {
/**
* Creates the URL suitable for pagination.
* This method is mainly called by pagers when creating URLs used to
* perform pagination. The default implementation is to call
* the controller's createUrl method with the page information.
* You may override this method if your URL scheme is not the same as
* the one supported by the controller's createUrl method.
* @param CController the controller that will create the actual URL
* @param integer the page that the URL should point to. This is a zero-based index.
* @return string the created URL
*/
public function createPageUrl($controller,$page)
{
$params=$this->params===null ? $_GET : $this->params;
// All of the pages get the page number parameter now, even if it's page one
$params[$this->pageVar]=$page+1;
return $controller->createUrl($this->route,$params);
}
}
?>
Then loaded both in my config by loading all the app. extensions:
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.helpers.*',
'application.extensions.*', ### <-- added this for loading extensions ###
),
Then I went and modified my list.php view to use the new pager:
<?php $this->widget('MyLinkPager',array('pages'=>$pages)); ?>
And then I went to my controller actionList and made the pagination use my new pagination class:
$pages=new MyPagination($count);
Hope this helps someone, if you got any trouble you can contact me and maybe I can help!