YII CLinkPager分頁類擴展增加顯示共多少頁
yii的分頁類CLinkPager默認是不支持顯示共x頁的,那么現(xiàn)在需求來了,要在分頁的后面顯示共多少頁,怎么辦喃?我們來看解決辦法
1、默認的CLinkPager顯示的效果
上面這里寫了css的樣式哈,我們來看pager代碼:
<div class="page-link">
<?php $this->widget('CLinkPager',array(
'header' => '',
'firstPageLabel' => '首頁',
'lastPageLabel' => '尾頁',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
'pages' => $pages,
'maxButtonCount'=>5,
'htmlOptions' => array('class' => 'page-link'), //分頁要使用的css樣式
));?>
</div>
2、我們來看想要的分頁類效果
也就是說后面增加顯示了共多少頁,這個怎么做到的喃?這里我稍微小小的擴展了一下widget組件CLinkPager,看上去也是非常的狠狠簡單吶,廢話不多少,來來先看代碼:
<?php
/**
* 分頁組建ClinkPager擴展
* @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' => '<',
* 'nextPageLabel' => '>',
* 'pages' => $pages,
* 'maxButtonCount'=>5,
* 'htmlOptions' => array('class' => 'page-tab-tog'),
* ));?>
* </div>
*/
class MLinkPager extends CLinkPager
{
//設置為true的時候,顯示共X頁,$this->forceTotalPage值優(yōu)先該值
public $mCountPage = false;
//是否強制顯示共x頁,設置為true時,$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ù)看怎么使用,首先呢,你需要先把上面的擴展MLinkPager原封不動的拷貝到本地的components目錄下的MlinkPager文件里,什么,你沒有這個文件,自己創(chuàng)建,^~^!好了以后咱們來看下view里面是怎么使用的,那是簡單的不能再過于簡單了。
<div class="page-tab-tog">
<?php $this->widget('MLinkPager',array(
'header' => '',
'firstPageLabel' => '首頁',
'lastPageLabel' => '尾頁',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
'pages' => $pages,
'maxButtonCount'=>5,
'mCountPage' => true, //!!!注意看這里,加一行代碼就ok了
'htmlOptions' => array('class' => 'page-tab-tog'),
));?>
</div>
什么?你剛睡醒眼神不好,沒看出來區(qū)別?注意看MLinkPager的配置項mCountPage,這個設置為true就萬事大吉了!
特別說明:如果你的列表沒有數(shù)據(jù)的話,分頁是不顯示頁碼的,但是如果有刁蠻產(chǎn)品要的需求是沒有列表數(shù)據(jù),但但但你必須得吧共0頁顯示出來,我們的MlinkPager只需要設置下配置項forceTotalPage為true即可,此時設置mCountPager無效了咯,具體詳細請看MlinkPage類,次類可自己再進行擴展
下面給大家介紹在在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、計算數(shù)據(jù)庫中總的條數(shù)
2、分頁大小
3、設置偏移量limit
在Yii中,分頁時會用這個類CDBcritria進行數(shù)據(jù)庫查詢很重要,這樣分頁很簡單。
相關(guān)文章
laravel withCount 統(tǒng)計關(guān)聯(lián)數(shù)量的方法
今天小編就為大家分享一篇laravel withCount 統(tǒng)計關(guān)聯(lián)數(shù)量的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
ThinkPHP5實現(xiàn)作業(yè)管理系統(tǒng)中處理學生未交作業(yè)與已交作業(yè)信息的方法
這篇文章主要介紹了ThinkPHP5實現(xiàn)作業(yè)管理系統(tǒng)中處理學生未交作業(yè)與已交作業(yè)信息的方法,涉及thinkPHP針對數(shù)據(jù)表的查詢與遍歷操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-11-11
Laravel框架實現(xiàn)調(diào)用百度翻譯API功能示例
這篇文章主要介紹了Laravel框架實現(xiàn)調(diào)用百度翻譯API功能,結(jié)合實例形式分析了基于Laravel框架的百度翻譯API調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
discuz免激活同步登入代碼修改方法(discuz同步登錄)
無需激活直接同步登入discuz,測試版本:discuz! x3,理論也支持2.5,大家測試再用吧2013-12-12
用PHP做了一個領(lǐng)取優(yōu)惠券活動的示例代碼
這篇文章主要介紹了用PHP做了一個領(lǐng)取優(yōu)惠券活動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07

