下面我總結了在Yii常用的一些yii分頁方式與實例代碼,這裡有普通分頁與ajax實現分頁,希望這篇文章對大家會有所幫助。
第一種:CListView分頁 物件形式的資料分頁
Controller:
public function actionAjax() { $criteria = new CDbCriteria(); //$criteria->order = 'news_id DESC'; $criteria->condition = 'user_id = 1'; $dataProvider = new CActiveDataProvider('News', array( 'pagination' => array( 'pageSize' => Yii::app()->params['pagesize'], 'pageVar' => Yii::app()->params['pagevar'], ), 'criteria' => $criteria, )); $this->render('view', array( 'dataProvider' => $dataProvider, )); }
View:
<?php $this->widget('zii.widgets.CListView', array( 'dataProvider' => $dataProvider, //数据 'itemView' => '_view', //显示的模版 'id' => Yii::app()->controller->id, 'itemsTagName' => 'ul', 'ajaxVar' => '', //默认为page或ajax 去掉后url更简洁 'htmlOptions' => array('class' => Yii::app()->controller->id), 'loadingCssClass' => 'loading', //默认为list-view-loading //'template' => '{summary}{sorter}{items}{pager}',//显示的顺序 //'ajaxUpdate' => false, //是否ajax分页 false或分页显示的容器id //'beforeAjaxUpdate' => 'before_ajax_update', //回调函数 在common.js里完成 //'afterAjaxUpdate' => 'after_ajax_update', 'emptyText' => ' <DIV class="alert alert-waning"> 暂无数据! </DIV> ', //无数据时显示内容 'pagerCssClass' => 'pagination', //分页的class 'pager' => array( 'selectedPageCssClass' => 'active', //当前页的class 'hiddenPageCssClass' => 'disabled', //禁用页的class 'header' => '', //分页前显示的内容 'maxButtonCount' => 10, //显示分页数量 'htmlOptions' => array('class' => ''), 'firstPageLabel' => '首页', 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', 'lastPageLabel' => '末页', ), )); ?>
第二種:CLinkPager 針對數組形式的資料分頁
三種: DAO實現分頁.Controller層:public function actionIndex() { $criteria = new CDbCriteria(); $criteria->order = 'news_id DESC'; $criteria->condition = 'user_id = 1'; $count = News::model()->count($criteria); $pages = new CPagination($count); $pages->pageSize = 10; $pages->applyLimit($criteria); $list = News::model()->findAll($criteria); $this->render('index', array('list' => $list, 'pages' => $pages)); }
<UL> <?php foreach ($list as $item): ?> <LI> <DIV class=page-header> <?php echo $item--->news_title; ?> </DIV> <DIV class=content> <?php echo $item--->news_intro; ?> </DIV> </LI> <?php endforeach; ?> </UL> <DIV class=pagination> <?php $this--->widget('CLinkPager', array( 'pages' => $pages, 'selectedPageCssClass' => 'active', //当前页的class 'hiddenPageCssClass' => 'disabled', //禁用页的class 'header' => '', //分页前显示的内容 'maxButtonCount' => 10, //显示分页数量 'htmlOptions' => array('class' => ''), 'firstPageLabel' => '首页', 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', 'lastPageLabel' => '末页', ) ); ?> </DIV>
public function actionReport() { $sql = "select remitdate, sum(rate) sumrate from td_delivery group by remitdate order by remitdate desc"; $criteria=new CDbCriteria(); $result = Yii::app()->db->createCommand($sql)->query(); $pages=new CPagination($result->rowCount); $pages->pageSize=2; $pages->applyLimit($criteria); $result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); $result->bindValue(':offset', $pages->currentPage*$pages->pageSize); $result->bindValue(':limit', $pages->pageSize); $posts=$result->query(); $this->render('report',array( 'posts'=>$posts, 'pages'=>$pages, )); }
<?php foreach($posts as $row):?> <?php echo CHtml::link($row["remitdate"],array('delivery/view','remitdate'=>$row["sumrate"]));?> <?php echo $row["sumrate"]."<br />" ?> <?php endforeach;?> <?php //分页widget代码: $this->widget('CLinkPager',array('pages'=>$pages)); ?>
/** * @var string attribute : 日运费 (统计用) * 需要对新增加的字段做个声明 */ public $dayrate; /* * 统计功能: 统计每日的运费 */ public function statistics() { $criteria = new CDbCriteria; $criteria->select = 'remitdate, sum(rate) AS dayrate'; $criteria->group = 'remitdate'; return new CActiveDataProvider(get_class($this), array( 'criteria'=>$criteria, 'sort'=>array( // 表头设置点击排序的字段 'attributes'=>array( 'remitdate', 'dayrate'=>array( 'asc'=>'dayrate', 'desc'=>'dayrate DESC', ) ), 'defaultOrder'=>'remitdate desc', ), )); }
ajax分頁有多種實作方法,比較傳統的就是在view中寫JS來實現,大概的就是這樣:
在view中js大概邏輯是這樣/** * 运单统计功能: * 按日期统计 */ public function actionReport() { $model=new Delivery('statistics'); $model->unsetAttributes(); // clear any default values $this->render('report',array( 'model'=>$model, )); }
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'delivery-grid', 'dataProvider'=>$model->statistics(), 'filter'=>$model, 'columns'=>array( 'remitdate', 'dayrate', array( 'class'=>'CButtonColumn', ), ), )); ?>
controller中:
$('#listview .yiiPager a').live('click',function(){ $.ajax({ url:$(this).attr('href'), success:function(html){ $('#listview').html(html); } }); return false;//阻止a标签 });
然後在view中:
if (Yii::app()->request->isAjaxRequest) { $this->renderPartial('_comments',array( 'model' => $model, 'comments' => $comments,//在局部视图中foreach得到每条数据 'pages' => $pages, )); Yii::app()->end(); }
這樣就實現了Ajax分頁,很方便。
希望本文所述對大家以Yii框架為基礎的PHP程式設計有所幫助。
更多Yii2分頁的使用及其擴充方法詳解相關文章請關注PHP中文網!