将yii自带的两个分页合二为一 [ 新手入门 ]
1、创建一个类文件CWetPager.php放到framework\web\widgets\pagers\文件下 代码如下:
<?php
/**
* CWetPager class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CWetPager displays a list of hyperlinks that lead to different pages of target.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CWetPager.php 2799 2011-01-01 19:31:13Z qiang.xue $
* @package system.web.widgets.pagers
* @since 1.0
*/
class CWetPager extends CBasePager
{
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 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();
public $promptText;
public $pageTextFormat;
/**
* 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';
if($this->promptText!==null)
$this->htmlOptions['prompt']=$this->promptText;
if(!isset($this->htmlOptions['onchange']))
$this->htmlOptions['onchange']="if(this.value!='') {window.location=this.value;};";
}
/**
* Executes the widget.
* This overrides the parent implementation by displaying the generated page buttons.
*/
public function run()
{
/*CWetPager 页码平铺*/
$this->registerClientScript();
$buttons=$this->createPageButtons();
if(empty($buttons))
return;
/*CListPager 页码下拉框*/
if(($pageCount=$this->getPageCount())<=1)
return;
$pages=array();
for($i=0;$i<$pageCount;++$i)
$pages[$this->createPageUrl($i)]=$this->generatePageText($i);
$selection=$this->createPageUrl($this->getCurrentPage());
echo $this->header;
echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons)); //页码平铺
echo CHtml::dropDownList($this->getId(),$selection,$pages,$this->htmlOptions);//页码下拉框
echo $this->footer;
}
/*wet*/
protected function generatePageText($page)
{
if($this->pageTextFormat!==null)
return sprintf($this->pageTextFormat,$page+1);
else
return $page+1;
}
/**
* 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,$currentPage<=0,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,$currentPage>=$pageCount-1,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. This could be 'page', 'first', 'last', 'next' or 'previous'.
* @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,$page,$class,$hidden,$selected)
{
if($hidden || $selected)
$class.=' '.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);
return '<li class="'.$class.'">'.CHtml::link($label,$this->createPageUrl($page)).'</li>';
}
/**
* @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);
}
return array($beginPage,$endPage);
}
/**
* 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.
* @since 1.0.2
*/
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);
}
}
2、在framework文件夹下分别修改YiiBase.php和yiilite.php 的 private static $_coreClasses
这个数组 添加
'CWetPager' => '/web/widgets/pagers/CWetPager.php'
,
'CLinkPager' => '/web/widgets/pagers/CLinkPager.php',
'CListPager' => '/web/widgets/pagers/CListPager.php',
'CWetPager' => '/web/widgets/pagers/CWetPager.php',
3、在视图页面的分页改为 'pager'=>array('class'=>'CWetPager')
,到这一步两个分页就合一了,如果还希望更新页面后返回上一页就在加入以下代码
视图加
'ajaxUpdate'=> false,//取消分页的ajax
控制器里的方法加[code]//返回上一页
Yii::app()->request->urlReferrer;
4、显示首页尾页
修改framework\web\widgets\pagers\pager.css把的代码隐藏就可以了。
ul.yiiPager .first,
ul.yiiPager .last
{
display:none;
}
共 0 条回复
没有找到数据。
wangerting1988 北京
注册时间:2011-08-10
最后登录:2014-06-09
在线时长:0小时20分
最后登录:2014-06-09
在线时长:0小时20分
- 粉丝4
- 金钱410
- 威望0
- 积分410