[Yii Framework] Add a link in CGridView

别等时光非礼了梦想. 提交于 2019-12-18 18:56:30

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))',
        )
,
    )
,

)); ?> 

 

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)))'
        )
,
    )
,

)); ?>  

Have fun with Yii!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!