Yii列表定义与使用分页方法小结(3种方法)
本文实例讲述了Yii列表定义与使用分页方法。分享给大家供大家参考,具体如下:
方法一:控制器定义
function actionIndex(){ $criteria = new CDbCriteria(); $count=Article::model()->count($criteria); $pages=new CPagination($count); // 返回前一页 $pages->pageSize=10; $pages->applyLimit($criteria); $models = Post::model()->findAll($criteria); $this->render('index', array( 'models' => $models, 'pages' => $pages )); }
视图定义:
<?php foreach($models as $model): ?> // 显示一个模型 <?php endforeach; ?> // 显示分页 <?php $this->widget('CLinkPager', array( 'pages' => $pages, )) ?>
方法二:控制器定义:
public function actionIndex() { $dataProvider=new CActiveDataProvider('News',array( 'criteria'=>array( 'condition'=>'status=1', 'order'=>'create_time DESC', 'with'=>array('author'), ), 'pagination'=>array( 'pageSize'=>20, ), )); $this->render('index',array( 'dataProvider'=>$dataProvider, )); }
视图文件:
<?php $this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view', )); ?>
方法三:视图文件
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'news-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'template'=>'{items}{summary}{pager}', <span style="white-space:pre"> </span> 'pager'=>array( 'class'=>'CLinkPager', 'header'=>'分页:', 'prevPageLabel'=>'上一页', 'nextPageLabel'=>'下一页', ), <span style="white-space:pre"> </span> 'summaryText'=>'页数:{pages}/{page}页', <span style="white-space:pre"> </span>'columns'=>array( 'id', array('name'=>'title', 'htmlOptions'=>array('width'=>'20%'), 'value'=>'mb_substr($data->title,0,10,"utf-8")', ), array('name'=>'content', 'htmlOptions'=>array('width'=>'20%'), 'value'=>'mb_substr(strip_tags($data->content),0,10,"utf-8")', ), array('name'=>'type', 'value'=>'News::model()->getNewsType($data->type)', ), 'user', array('name'=>'status', 'value'=>'News::model()->getNewsStatus($data->status)', ), array( 'class'=>'CButtonColumn', ), ), ));
数据模型类:
public function search() { $criteria=new CDbCriteria; $criteria->compare('id',$this->id); $criteria->compare('title',$this->title,true); $criteria->compare('content',$this->content,true); $criteria->compare('type',$this->type); $criteria->compare('user',$this->user,true); $criteria->compare('status',$this->status); $criteria->compare('create_data',$this->create_data,true); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'pagination'=>array( 'pageSize'=>50, ), )); }
更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
相关推荐
K先生 2020-11-10
wushengyong 2020-10-28
西瓜皮儿的皮儿 2020-09-07
molong0 2020-08-03
jokewinl 2020-07-18
田有朋 2020-07-05
80530895 2020-07-05
javamagicsun 2020-06-21
liangwenrong 2020-06-17
molong0 2020-06-16
xiuyangsong 2020-06-03
jaylong 2020-06-02
EdShao 2020-05-29
晨曦之星 2020-05-29
dreamhua 2020-05-10
smalllove 2020-05-09
liqiancao 2020-05-08
Dullonjiang 2020-05-06
GechangLiu 2020-05-04