[Yii Framework] Add a link in CGridView
When we using CGridView in Yii, we may want to a link as a column. So in common sense, we will use CLinkColumn to do that, as these code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'username',
'class'=>'CLinkColumn',
'labelExpression'=>'$data->username',
'urlExpression'=>'Yii::app()->createUrl("admin/user", array("id"=>$data->id))',
),
),
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'username',
'class'=>'CLinkColumn',
'labelExpression'=>'$data->username',
'urlExpression'=>'Yii::app()->createUrl("admin/user", array("id"=>$data->id))',
),
),
)); ?>
But we can not sort the username with this way. Thanks for Yii, we can use another way to do it. Here are the code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name' => 'username',
'type' => 'raw',
'value' => 'CHtml::link($data->username,Yii::app()->createUrl("admin/user/view", array("id"=>$data->id)))'
),
),
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name' => 'username',
'type' => 'raw',
'value' => 'CHtml::link($data->username,Yii::app()->createUrl("admin/user/view", array("id"=>$data->id)))'
),
),
)); ?>
Have fun with Yii!