Yii使用CLinkPager分頁(yè)實(shí)例詳解
本文主要講解了YII中使用CLinkPager分頁(yè)的方法,這里我們采用物件的形式來(lái)定義分頁(yè):
首先在components中自定義LinkPager,并繼承CLinkPager
具體代碼如下:
<?php
/**
* CLinkPager class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CLinkPager displays a list of hyperlinks that lead to different pages of target.
*
* @version $Id$
* @package system.web.widgets.pagers
* @since 1.0
*/
class LinkPager extends CLinkPager
{
const CSS_TOTAL_PAGE='total_page';
const CSS_TOTAL_ROW='total_row';
/**
* @var string the text label for the first page button. Defaults to '<< First'.
*/
public $totalPageLabel;
/**
* @var string the text label for the last page button. Defaults to 'Last >>'.
*/
public $totalRowLabel;
/**
* Creates the page buttons.
* @return array a list of page buttons (in HTML code).
*/
protected function createPageButtons()
{
$this->maxButtonCount=8;
$this->firstPageLabel="首頁(yè)";
$this->lastPageLabel='末頁(yè)';
$this->nextPageLabel='下一頁(yè)';
$this->prevPageLabel='上一頁(yè)';
$this->header="";
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);
// 頁(yè)數(shù)統(tǒng)計(jì)
$buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false);
// 條數(shù)統(tǒng)計(jì)
$buttons[]=$this->createTotalButton("共{$this->getItemCount()}條",self::CSS_TOTAL_ROW,false,false);
return $buttons;
}
protected function createTotalButton($label,$class,$hidden,$selected)
{
if($hidden || $selected)
$class.=' '.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);
return '<li class="'.$class.'">'.CHtml::label($label,false).'</li>';
}
/**
* 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('application.components.views.LinkPager.pager').'.css');
Yii::app()->getClientScript()->registerCssFile($url);
}
}
定義CSS樣式
/**
* 翻頁(yè)樣式
*/
.page_blue{
margin: 3px;
padding: 3px;
text-align: center;
font: 12px verdana, arial, helvetica, sans-serif;
}
ul.bluePager,ul.yiiPager
{
font-size:11px;
border:0;
margin:0;
padding:0;
line-height:100%;
display:inline;
text-aligin:center;
}
ul.bluePager li,ul.yiiPager li
{
display:inline;
}
ul.bluePager a:link,ul.yiiPager a:link,
ul.bluePager a:visited,ul.yiiPager a:visited,
ul.bluePager .total_page label,ul.yiiPager .total_page label,
ul.bluePager .total_row label,ul.yiiPager .total_row label
{
border: #ddd 1px solid;
color: #888888 !important;
padding:2px 5px;
text-decoration:none;
}
ul.bluePager .page a,ul.yiiPager .page a
{
font-weight:normal;
}
ul.bluePager a:hover,ul.yiiPager a:hover
{
color:#FFF !important; border:#156a9a 1px solid; background-color:#2b78a3
}
ul.bluePager .selected a,ul.yiiPager bluePager .selected a
{
color:#3aa1d0 !important;
border: 1px solid #3aa1d0;
}
ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover
{
color:#FFF !important;
}
ul.bluePager .hidden a,ul.yiiPager .hidden a
{
border:solid 1px #DEDEDE;
color:#888888;
}
ul.bluePager .hidden,ul.yiiPager .hidden
{
display:none;
}
controller中操作:
//分頁(yè)操作
$criteria=new CDbCriteria;
$criteria->order='id DESC';
$criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality');
$criteria->condition=$sql;
$total = Video::model()->count($criteria);
$pages = new CPagination($total);
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);
$list = Video::model()->findAll($criteria);
$title = CommonClass::model()->find(array(
'select'=>array('cname'),
'condition'=>'id = '.$id,
));
$this->render('application.views.video.list',array(
'array'=>$array,
'arr'=>$arr,
'result'=>$result,
'list'=>$list,
'pages'=>$pages,
'title'=>$title,
));
在views/video/list.php中引用:
<?php
$this->widget('LinkPager', array('pages' => $pages,));
?>
- yii分頁(yè)組件用法實(shí)例分析
- Yii2.0框架實(shí)現(xiàn)帶分頁(yè)的多條件搜索功能示例
- Yii2.0小部件GridView(兩表聯(lián)查/搜索/分頁(yè))功能的實(shí)現(xiàn)代碼
- yii2實(shí)現(xiàn)分頁(yè),帶搜索的分頁(yè)功能示例
- yii2分頁(yè)之實(shí)現(xiàn)跳轉(zhuǎn)到具體某頁(yè)的實(shí)例代碼
- Yii2分頁(yè)的使用及其擴(kuò)展方法詳解
- Yii分頁(yè)用法實(shí)例詳解
- Yii列表定義與使用分頁(yè)方法小結(jié)(3種方法)
- 詳解Yii實(shí)現(xiàn)分頁(yè)的兩種方法
- Yii1.1中通過(guò)Sql查詢進(jìn)行的分頁(yè)操作方法
- yii使用bootstrap分頁(yè)樣式的實(shí)例
- YII2框架中分頁(yè)組件的使用方法示例
相關(guān)文章
yii實(shí)現(xiàn)model添加默認(rèn)值的方法(2種方法)
這篇文章主要介紹了yii實(shí)現(xiàn)model添加默認(rèn)值的方法,結(jié)合實(shí)例分析了在rules()方法及在beforeSave()方法中設(shè)定兩種實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
laravel框架中間件 except 和 only 的用法示例
這篇文章主要介紹了laravel框架中間件 except 和 only 的用法,簡(jiǎn)單說(shuō)明了中間件 except 和 only的功能,并結(jié)合實(shí)例形式分析了laravel框架中間件 except 和 only 相關(guān)使用技巧,需要的朋友可以參考下2019-07-07
解決laravel5.4下的group by報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決laravel5.4下的group by報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
tp5.1 實(shí)現(xiàn)setInc字段自動(dòng)加1
今天小編就為大家分享一篇tp5.1 實(shí)現(xiàn)setInc字段自動(dòng)加1示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
利用php+mysql來(lái)做一個(gè)功能強(qiáng)大的在線計(jì)算器
有天在努力的搜索計(jì)算器,發(fā)現(xiàn)都是JavaScript,而且要一個(gè)個(gè)地點(diǎn)擊,并且不能記錄,輸入計(jì)算式子時(shí)容易出錯(cuò),于是就想了想該怎樣才能讓它好用點(diǎn)呢,能夠用鍵盤(pán)直接輸入。2010-10-10
thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能
這篇文章主要介紹了thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了修改QQ登陸接口并整合進(jìn)thinkPHP5項(xiàng)目中的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
用HTML/JS/PHP方式實(shí)現(xiàn)頁(yè)面延時(shí)跳轉(zhuǎn)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇用HTML/JS/PHP方式實(shí)現(xiàn)頁(yè)面延時(shí)跳轉(zhuǎn)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07

