How to display rows of CGridView as a link

Darshit Gajjar picture Darshit Gajjar · Jan 3, 2012 · Viewed 24.6k times · Source

I want to display CGridView component with all records as a link to its related Update page.

I want the functionality of edit button at the end of every row, applied to whole row.

If I click anywhere on a particular row, it will redirect me to its update screen respectively.

Is it possible in Yii's CGridView ?

Answer

Rajat Singhal picture Rajat Singhal · Jan 3, 2012

CGridView

'columns'=>array(
'id',
//'full_name',
//'username',
array(
        'name'  => 'full_name',
        'value' => 'CHtml::link($data->full_name, Yii::app()
 ->createUrl("user/view",array("id"=>$data->primaryKey)))',
        'type'  => 'raw',
    ),
array(
        'name'  => 'username',
        'value' => 'CHtml::link($data->username,Yii::app()->createUrl("user/view",array("id"=>$data->primaryKey)))',
        'type'  => 'raw',
    ),
'email',

To make whole row an link try this...

<?php $this->widget('zii.widgets.grid.CGridView', array(
...
'htmlOptions'=>array('style'=>'cursor: pointer;'),
'selectionChanged'=>"function(id){window.location='" . Yii::app()->urlManager->createUrl('controller/action', array('id'=>'')) . "' + $.fn.yiiGridView.getSelection(id);}",
...
)); ?>