yii中自动生成curd,怎样控制分页条数

比如说你admin页面的CGridView,在数据源中的dataProvider返回前设置一下,
model层代码

$dataProvider=new CActiveDataProvider($this, array('criteria'=>$criteria));
$dataProvider->getPagination()->pageSize=5;//你想每页显示的条数
return $dataProvider;

controller层代码
$this->render('admin',array(
'model'=>$dataProvider
));
view层代码
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'sys-order-grid',
'dataProvider'=>$model,
 

本文实例讲述了Yii列表定义与使用分页方法。分享给大家供大家参考,具体如下:

方法一:控制器定义

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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
   ));
}

视图定义:

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

方法二:控制器定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 ,
   ));
}

视图文件:

?
1
2
3
4
<?php $this ->widget( 'zii.widgets.CListView' , array (
   'dataProvider' => $dataProvider ,
   'itemView' => '_view' ,
)); ?>

方法三:视图文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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' ,
     ),
   ),
));

数据模型类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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,
     ),
   ));
}
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值