YII CLinkPager分頁類擴(kuò)展增加顯示共多少頁
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' => '<', 'nextPageLabel' => '>', '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' => '<', * 'nextPageLabel' => '>', * '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' => '<', 'nextPageLabel' => '>', '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)單。
- Yii視圖CGridView實(shí)現(xiàn)操作按鈕定義地址示例
- Yii CGridView用法實(shí)例詳解
- Yii視圖CGridView列表用法實(shí)例分析
- Yii中CGridView禁止列排序的設(shè)置方法
- Yii中CGridView實(shí)現(xiàn)批量刪除的方法
- Yii中CGridView關(guān)聯(lián)表搜索排序方法實(shí)例詳解
- Yii把CGridView文本框換成下拉框的方法
- Yii2分頁的使用及其擴(kuò)展方法詳解
- yii框架分類樹擴(kuò)展示例
- PHP的Yii框架中YiiBase入口類的擴(kuò)展寫法示例
- YII視圖整合kindeditor擴(kuò)展的方法
- Yii框架擴(kuò)展CGridView增加導(dǎo)出CSV功能的方法
相關(guān)文章
php使用curl檢測(cè)網(wǎng)頁是否被百度收錄的示例分享
這篇文章主要介紹了php使用curl檢測(cè)網(wǎng)頁是否被百度收錄的示例,需要的朋友可以參考下2014-01-01關(guān)于laravel-admin ueditor 集成并解決刷新的問題
今天小編就為大家分享一篇關(guān)于laravel-admin ueditor 集成并解決刷新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10php多種形式發(fā)送郵件(mail qmail郵件系統(tǒng) phpmailer類)
這篇文章主要介紹了php多種形式發(fā)送郵件的方法,包括使用mail()函數(shù)、使用管道的形式、使用phpmailer類等方法,大家參考使用吧2014-01-01WAMP環(huán)境中擴(kuò)展oracle函數(shù)庫(kù)(oci)
本文給大家介紹的是在windows環(huán)境下為php環(huán)境擴(kuò)展Oracle函數(shù)庫(kù)的過程,十分的詳細(xì),有需要的小伙伴可以參考下。2015-06-06php框架CodeIgniter主從數(shù)據(jù)庫(kù)配置方法分析
這篇文章主要介紹了php框架CodeIgniter主從數(shù)據(jù)庫(kù)配置方法,結(jié)合實(shí)例形式分析了CodeIgniter框架主從數(shù)據(jù)庫(kù)配置方法、模型model與控制器調(diào)用操作技巧以及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-05-05