YII CLinkPager分頁類擴充增加顯示共多少頁_php執行個體

來源:互聯網
上載者:User
yii的分頁類CLinkPager預設是不支援顯示共x頁的,那麼現在需求來了,要在分頁的後面顯示共多少頁,怎麼辦喃?我們來看解決辦法

1、預設的CLinkPager顯示的效果

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

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

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

也就是說後面增加顯示了共多少頁,這個怎麼做到的喃?這裡我稍微小小的擴充了一下widget組件CLinkPager,看上去也是非常的狠狠簡單呐,廢話不多少,來來先看代碼:

<?php/*** 分頁組建ClinkPager擴充* @description page-tab-tog為分頁的樣式class* @author <[]>* @time 2016-01-29* @example* * <?php $this->widget('MLinkPager',array(* 'header' => '',* 'firstPageLabel' => '首頁',* 'lastPageLabel' => '尾頁',* 'prevPageLabel' => '<',* 'nextPageLabel' => '>',* 'pages' => $pages,* 'maxButtonCount'=>5,* 'htmlOptions' => array('class' => 'page-tab-tog'),* ));?>* */class MLinkPager extends CLinkPager{//設定為true的時候,顯示共X頁,$this->forceTotalPage值優先該值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;}}

有人說了,一看那麼一堆代碼,頭疼,你這玩意怎麼能以最快的速度見到效果呢?來來我們繼續看怎麼使用,首先呢,你需要先把上面的擴充MLinkPager原封不動的拷貝到本地的components目錄下的MlinkPager檔案裡,什麼,你沒有這個檔案,自己建立,^~^!好了以後咱們來看下view裡面是怎麼使用的,那是簡單的不能再過於簡單了。

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

什嗎?你剛睡醒眼神不好,沒看出來區別?注意看MLinkPager的配置項mCountPage,這個設定為true就萬事大吉了!

特別說明:如果你的列表沒有資料的話,分頁是不顯示頁碼的,但是如果有刁蠻產品要的需求是沒有列表資料,但但但你必須得吧共0頁顯示出來,我們的MlinkPager只需要設定下配置項forceTotalPage為true即可,此時設定mCountPager無效了咯,具體詳細請看MlinkPage類,次類可自己再進行擴充

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

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

在控制器中:

1、

$criteria = new CDbCriteria(); //new cdbcriteria資料庫
$criteria->id = 'id ASC'; //定序$count = Exchange::model()->count($criteria);$pager = new CPagination($count);$pager->pageSize=30;$pager->applyLimit($criteria);$categoryInfo = Category::model()->findAll($criteria); //根據條件查詢

  2、

$criteria = new CDbCriteria();$criteria->order = 'id ASC';$criteria->addCondition('status=1'); //根據條件查詢$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中傳入參數:

array("pages" => $pager)

 視圖中加入:

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

  分頁思想:

1、計算資料庫中總的條數

2、分頁大小

3、設定位移量limit

在Yii中,分頁時會用這個類CDBcritria進行資料庫查詢很重要,這樣分頁很簡單。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.