欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

YII CLinkPager分頁類擴(kuò)展增加顯示共多少頁

 更新時(shí)間:2016年01月29日 09:14:26   投稿:mrr  
yii的分頁類CLinkPager默認(rèn)是不支持顯示共x頁的,那么現(xiàn)在接的項(xiàng)目有這樣的需求,怎么辦呢?下面通過本文給大家介紹YII CLinkPager分頁類擴(kuò)展增加顯示共多少頁的實(shí)例代碼,需要的朋友參考下吧

yii的分頁類CLinkPager默認(rèn)是不支持顯示共x頁的,那么現(xiàn)在需求來了,要在分頁的后面顯示共多少頁,怎么辦喃?我們來看解決辦法

1、默認(rèn)的CLinkPager顯示的效果

上面這里寫了css的樣式哈,我們來看pager代碼:

<div class="page-link">
<?php $this->widget('CLinkPager',array(
'header' => '',
'firstPageLabel' => '首頁',
'lastPageLabel' => '尾頁',
'prevPageLabel' => '&lt;',
'nextPageLabel' => '&gt;',
'pages' => $pages,
'maxButtonCount'=>5,
'htmlOptions' => array('class' => 'page-link'), //分頁要使用的css樣式
));?>
</div>

2、我們來看想要的分頁類效果

也就是說后面增加顯示了共多少頁,這個(gè)怎么做到的喃?這里我稍微小小的擴(kuò)展了一下widget組件CLinkPager,看上去也是非常的狠狠簡(jiǎn)單吶,廢話不多少,來來先看代碼:

<?php
/**
* 分頁組建ClinkPager擴(kuò)展
* @description page-tab-tog為分頁的樣式class
* @author <[<xm 杭州>]>
* @time 2016-01-29
* @example
* <div class="page-tab-tog">
* <?php $this->widget('MLinkPager',array(
* 'header' => '',
* 'firstPageLabel' => '首頁',
* 'lastPageLabel' => '尾頁',
* 'prevPageLabel' => '&lt;',
* 'nextPageLabel' => '&gt;',
* 'pages' => $pages,
* 'maxButtonCount'=>5,
* 'htmlOptions' => array('class' => 'page-tab-tog'),
* ));?>
* </div>
*/
class MLinkPager extends CLinkPager
{
//設(shè)置為true的時(shí)候,顯示共X頁,$this->forceTotalPage值優(yōu)先該值
public $mCountPage = false;
//是否強(qiáng)制顯示共x頁,設(shè)置為true時(shí),$this->mCountPage和$this->getPageRange()無效
public $forceTotalPage = false;
public function init()
{
}
public function run()
{
$this->registerClientScript();
$buttons=$this->createPageButtons();
list($beginPage,$endPage)=$this->getPageRange();
if ($this->forceTotalPage)
{
$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'頁');
}
else
{
if ($this->mCountPage && $endPage > 0)
{
$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'頁');
}
}
if(empty($buttons))
return;
echo $this->header;
echo CHtml::tag('div',$this->htmlOptions,implode("\n",$buttons));
echo $this->footer;
}
}

有人說了,一看那么一堆代碼,頭疼,你這玩意怎么能以最快的速度見到效果呢?來來我們繼續(xù)看怎么使用,首先呢,你需要先把上面的擴(kuò)展MLinkPager原封不動(dòng)的拷貝到本地的components目錄下的MlinkPager文件里,什么,你沒有這個(gè)文件,自己創(chuàng)建,^~^!好了以后咱們來看下view里面是怎么使用的,那是簡(jiǎn)單的不能再過于簡(jiǎn)單了。

<div class="page-tab-tog">
<?php $this->widget('MLinkPager',array(
'header' => '',
'firstPageLabel' => '首頁',
'lastPageLabel' => '尾頁',
'prevPageLabel' => '&lt;',
'nextPageLabel' => '&gt;',
'pages' => $pages,
'maxButtonCount'=>5,
'mCountPage' => true, //!!!注意看這里,加一行代碼就ok了
'htmlOptions' => array('class' => 'page-tab-tog'),
));?>
</div>

什么?你剛睡醒眼神不好,沒看出來區(qū)別?注意看MLinkPager的配置項(xiàng)mCountPage,這個(gè)設(shè)置為true就萬事大吉了!

特別說明:如果你的列表沒有數(shù)據(jù)的話,分頁是不顯示頁碼的,但是如果有刁蠻產(chǎn)品要的需求是沒有列表數(shù)據(jù),但但但你必須得吧共0頁顯示出來,我們的MlinkPager只需要設(shè)置下配置項(xiàng)forceTotalPage為true即可,此時(shí)設(shè)置mCountPager無效了咯,具體詳細(xì)請(qǐng)看MlinkPage類,次類可自己再進(jìn)行擴(kuò)展

下面給大家介紹在在yii中使用分頁

yii中使用分頁很方便,如下兩種方法:

在控制器中:

1、

$criteria = new CDbCriteria(); //new cdbcriteria數(shù)據(jù)庫(kù)<br>$criteria->id = 'id ASC'; //排序規(guī)則
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria);
$categoryInfo = Category::model()->findAll($criteria); //根據(jù)條件查詢

  2、

$criteria = new CDbCriteria();
$criteria->order = 'id ASC';
$criteria->addCondition('status=1'); //根據(jù)條件查詢
$criteria->addCondition('exchange_status=0');
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria); 
$exchangeInfo = Exchange::model()->findAll($criteria); 

 render中傳入?yún)?shù):

array("pages" => $pager)

 視圖中加入:

$this->widget('CLinkPager',array(
'header'=>'',
'firstPageLabel' => '首頁',
'lastPageLabel' => '末頁',
'prevPageLabel' => '上一頁',
'nextPageLabel' => '下一頁',
'pages' => $pages,
'maxButtonCount'=>8,
)
); 

  分頁思想:

1、計(jì)算數(shù)據(jù)庫(kù)中總的條數(shù)

2、分頁大小

3、設(shè)置偏移量limit

在Yii中,分頁時(shí)會(huì)用這個(gè)類CDBcritria進(jìn)行數(shù)據(jù)庫(kù)查詢很重要,這樣分頁很簡(jiǎn)單。

相關(guān)文章

最新評(píng)論