溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-09-29 11:38:34 來源:億速云 閱讀:135 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“YII CLinkPager分頁類擴(kuò)展增加如何顯示共多少頁”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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,看上去也是非常的狠狠簡單吶,廢話不多少,來來先看代碼:

<?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里面是怎么使用的,那是簡單的不能再過于簡單了。

<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ì)請看MlinkPage類,次類可自己再進(jìn)行擴(kuò)展

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

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

在控制器中:

1、

$criteria = new CDbCriteria(); //new cdbcriteria數(shù)據(jù)庫<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ù)庫中總的條數(shù)

2、分頁大小

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

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

“YII CLinkPager分頁類擴(kuò)展增加如何顯示共多少頁”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI