加载中...

Yii-列表- 定义分页,分页方法


方法一:控制器定义
  1. function actionIndex(){
  2. $criteria = new CDbCriteria();
  3. $count=Article::model()->count($criteria);
  4. $pages=new CPagination($count);
  5.  
  6. // 返回前一页
  7. $pages->pageSize=10;
  8. $pages->applyLimit($criteria);
  9. $models = Post::model()->findAll($criteria);
  10.  
  11. $this->render('index', array(
  12. 'models' => $models,
  13. 'pages' => $pages
  14. ));
  15. }

视图定义
  1. <?php foreach($models as $model): ?>
  2. // 显示一个模型
  3. <?php endforeach; ?>
  4.  
  5. // 显示分页
  6. <?php $this->widget('CLinkPager', array(
  7. 'pages' => $pages,
  8. )) ?>

方法二:控制器定义

  1. public function actionIndex()
  2. {
  3. $dataProvider=new CActiveDataProvider('News',array(
  4. 'criteria'=>array(
  5. 'condition'=>'status=1',
  6. 'order'=>'create_time DESC',
  7. 'with'=>array('author'),
  8. ),
  9. 'pagination'=>array(
  10. 'pageSize'=>20,
  11. ),
  12. ));
  13. $this->render('index',array(
  14. 'dataProvider'=>$dataProvider,
  15. ));
  16. }
视图文件
  1. <?php $this->widget('zii.widgets.CListView', array(
  2. 'dataProvider'=>$dataProvider,
  3. 'itemView'=>'_view',
  4. )); ?>

方法三:视图文件

  1. <?php $this->widget('zii.widgets.grid.CGridView', array(
  2. 'id'=>'news-grid',
  3. 'dataProvider'=>$model->search(),
  4. 'filter'=>$model,
  5. 'template'=>'{items}{summary}{pager}',
  6. 'pager'=>array(
  7. 'class'=>'CLinkPager',
  8. 'header'=>'分页:',
  9. 'prevPageLabel'=>'上一页',
  10. 'nextPageLabel'=>'下一页',
  11. ),
  12. 'summaryText'=>'页数:{pages}/{page}页',
  13. 'columns'=>array(
  14. 'id',
  15. array('name'=>'title',
  16. 'htmlOptions'=>array('width'=>'20%'),
  17. 'value'=>'mb_substr($data->title,0,10,"utf-8")',
  18. ),
  19. array('name'=>'content',
  20. 'htmlOptions'=>array('width'=>'20%'),
  21. 'value'=>'mb_substr(strip_tags($data->content),0,10,"utf-8")',
  22. ),
  23. array('name'=>'type',
  24. 'value'=>'News::model()->getNewsType($data->type)',
  25. ),
  26. 'user',
  27. array('name'=>'status',
  28. 'value'=>'News::model()->getNewsStatus($data->status)',
  29. ),
  30. array(
  31. 'class'=>'CButtonColumn',
  32. ),
  33. ),
  34. ));

数据模型类

  1. public function search()
  2. {
  3. $criteria=new CDbCriteria;
  4.  
  5. $criteria->compare('id',$this->id);
  6. $criteria->compare('title',$this->title,true);
  7. $criteria->compare('content',$this->content,true);
  8. $criteria->compare('type',$this->type);
  9. $criteria->compare('user',$this->user,true);
  10. $criteria->compare('status',$this->status);
  11. $criteria->compare('create_data',$this->create_data,true);
  12.  
  13. return new CActiveDataProvider($this, array(
  14. 'criteria'=>$criteria,
  15. 'pagination'=>array(
  16. 'pageSize'=>50,
  17. ),
  18. ));
  19. }



还没有评论.