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; }
时间: 2024-11-09 00:33:57