- CGridView is one of Zii Components
- There is other than CGridView, CDetailView, CListView. Look here.
- CGridView use DataProvider which define in controller
- CGridView display data in tables.
- Supports paginating, sorting, searching.
- Also support view, update, delete method
/**
 * Lists all models.
 */
 public function actionIndex()
 {
$dataProvider=new CActiveDataProvider(‘User’,
array(
‘pagination’=>array(
‘pageSize’=>10,
),
‘sort’=>array(
‘defaultOrder’=> array(‘id’=>false),
)
));
$this->render(‘index’,array(
 ‘dataProvider’=>$dataProvider,
 ));
 }
[/php]
Then just put code below at the view (currently index.php). Minimal code to use CGridView
[php]
$this->widget(‘zii.widgets.grid.CGridView’, array(
‘dataProvider’=>$dataProvider,
 ));
[/php]
The code above will view all data from the selected table / model.
If we would like to view selected column only. We could do like this
[php]
$this->widget(‘zii.widgets.grid.CGridView’, array(
 ‘dataProvider’=>$dataProvider,
 ‘columns’=>array(
 ‘id’,
 ‘username’,
 //’password’,
 ’email’,
 ‘joined’,
 ),
));
[/php]
To allow view, update and delete action. We add the CButtonColumn like this.
[php]
$this->widget(‘zii.widgets.grid.CGridView’, array(
‘dataProvider’=>$dataProvider,
‘columns’=>array(
‘id’,
‘username’,
//’password’,
’email’,
‘joined’,
array(
 ‘class’=>’CButtonColumn’,
 ),
),
));
[/php]
To customize just view button only, we do like this
[php]
		array(
			‘class’=>’CButtonColumn’,
            ‘template’=> ‘{view}’,
		),
[/php]
To go customize more detail, we need to use RBAC where we need to define ‘deleteAccess’ rules. We can customize like this
[php]
array(
                        ‘class’=>’CButtonColumn’,
            ‘template’=>'{update}{delete}’,
            ‘buttons’=>array(
                ‘update’=>array(
                    ‘label’=>’Update details’,
                    ‘url’=>’Yii::app()->createUrl("whatever/update", array("id"=>$data->id))’,
                ),
                ‘delete’ => array(
                    ‘label’=>’Delete’,
                    ‘url’=>’Yii::app()->createUrl("whatever/delete", array("id"=>$data->id)’,
                    ‘visible’=>Yii::app()->user->checkAccess(‘deleteAccess’),
                    ),
                ),
            ),
[/php]

Leave a Reply