码迷,mamicode.com
首页 > 其他好文 > 详细

Yii多条数据展示挂件之CGridView

时间:2014-10-23 14:02:03      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   使用   sp   数据   div   

Yii多条数据展示挂件CGridView的简单使用

第一种:

在User的控制器中

public function actionIndex() {

    $criteria = new CDbCriteria();

    //关联关系

    $criteria -> with = array(‘city‘,‘user_info‘);

    //创建查询数据的对象

    $dataProvider = new CActiveDataProvider(‘User‘,array(

      ‘criteria‘ => $criteria,

      ‘pagination‘=> array(

        ‘pageSize‘ =>5

      ),  

    ));

    //渲染视图

    $this->render(‘index‘,array(

      ‘dataProvider‘ => $dataProvider,

    ));

  }

在视图user/index.php中

$this->widget(‘zii.widgets.grid.CGridView‘,array(

    ‘dataProvider‘=> $dataProvider,

    ‘ajaxUpdate‘ =>true,  //是否开启ajax切换上下页

    ‘columns‘ => array(

      //定义全选和全不选

      array(

        ‘class‘=>‘CCheckBoxColumn‘,

        ‘name‘=>‘id‘,

        ‘value‘=>$data->id,

        ‘id‘=>‘select‘,

        ‘htmlOptions‘=>array(‘class‘=>‘checkbox_column‘),

        ‘selectableRows‘=>2,

      ),

      ‘id‘,

      ‘name‘,

      ‘city.name‘,

      array(

        ‘name‘=>‘create_time‘,

        ‘value‘=>‘data("Y-m-d H:i",$data->create_time)‘,

      ),

      array(

        ‘name‘=>‘user_info‘,

        ‘value‘=>‘$data->user_info->info‘,

      ),

      array(

        ‘column‘=>array(

          ‘class‘=>‘CButtonColumn‘,

          ‘template‘=>‘{view} {update} {delete} {log}‘,

          ‘deleteButtonImageUrl‘=>Yii::app()->request->baseUrl.‘/assets/images/button.jpg‘,

          ‘buttons‘=>array(

            ‘log‘=>array(

              ‘label‘=>‘日志‘,

              ‘url‘=>‘Yii::app()->controller->createUrl("/user/delete",array(‘id‘=>$data->id))‘,

              ‘imageUrl‘=>Yii::app()->request->baseUrl.‘/assets/images/log.jpg‘,

              ‘options‘=>array(‘target‘=>‘_blank‘),

              ‘visible‘=>‘$data->isShow()‘

            ),

          ),

        ),

      ),

    ),

  ));

在User.php的模型中 

public function isShow(){

     return $this->id % 2 == 0;

  }

 

 

第二种:

在UserController.php控制器中  

public function actionIndex() {

    $model = new User(‘search‘);

    //清空默认值

    $model -> unsetAttributes();

    //渲染视图

    $this->render(‘index‘,array(

      ‘model‘=>$model,

    ));

  }

在user/index.php视图中

$this->widget(‘zii.widgets.grid.CGridView‘,array(

    ‘dataProvider‘=> $model->search(),

    ‘ajaxUpdate‘ =>true,  //是否开启ajax切换上下页

    ‘columns‘ => array(

      //定义全选和全不选

      array(

        ‘class‘=>‘CCheckBoxColumn‘,

        ‘name‘=>‘id‘,

        ‘value‘=>$data->id,

        ‘id‘=>‘select‘,

        ‘htmlOptions‘=>array(‘class‘=>‘checkbox_column‘),

        ‘selectableRows‘=>2,

      ),

      ‘id‘,

      ‘name‘,

      ‘city.name‘,

      array(

        ‘name‘=>‘create_time‘,

        ‘value‘=>‘data("Y-m-d H:i",$data->create_time)‘,

      ),

      array(

        ‘name‘=>‘user_info‘,

        ‘value‘=>‘$data->user_info->info‘,

      ),

      array(

        ‘column‘=>array(

          ‘class‘=>‘CButtonColumn‘,

          ‘template‘=>‘{view} {update} {delete} {log}‘,

          ‘deleteButtonImageUrl‘=>Yii::app()->request->baseUrl.‘/assets/images/button.jpg‘,

          ‘buttons‘=>array(

            ‘log‘=>array(

              ‘label‘=>‘日志‘,

              ‘url‘=>‘Yii::app()->controller->createUrl("/user/delete",array(‘id‘=>$data->id))‘,

              ‘imageUrl‘=>Yii::app()->request->baseUrl.‘/assets/images/log.jpg‘,

              ‘options‘=>array(‘target‘=>‘_blank‘),

              ‘visible‘=>‘$data->isShow()‘

            ),

          ),

        ),

      ),

    ),

  ));

在User.php模型中:

public function search() {

    $criteria = new CDbCriteria;

    $criteria->with = ‘city‘;
    $criteria->with = ‘user_info‘;
    $criteria->compare(‘id‘, $this->id);
    $criteria->compare(‘name‘, $this->name, true);
    $criteria->compare(‘city_id‘, $this->city_id, true);
    $criteria->compare(‘create_time‘, $this->create_time, true);
    $criteria->compare(‘update_time‘, $this->update_time, true);

    return new CActiveDataProvider($this, array(
      ‘criteria‘ => $criteria,
      ‘pagination‘=>array(
        ‘pageSize‘=>5,
      ),
    ));
  }

  public function isShow(){

     return $this->id % 2 == 0;

  }

 

Yii多条数据展示挂件之CGridView

标签:style   blog   color   io   ar   使用   sp   数据   div   

原文地址:http://www.cnblogs.com/liuwz/p/4045501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!