How To Merge Two Column Into One Column Of Cgridview

hi experts,

I want to do like this in CGridview:




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$model->search(),

	'filter'=>$model,'hideHeader'=>false,

	'columns'=>array(

		array(

			'name'=>'title',

			'type'=>'html',

			'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'.' '.$data->create_time,

		),

		array(

			'name'=>'create_time',

			'type'=>'datetime',

			'filter'=>false,

		),




it threw the exception like: Undefined variable: data

You can do this by creating a function i.e. MyValue and then call it.

MyModel




public function getMyValue(){

return $this->v1.' '.$this->v2;

}



in view




                array(

                        'name'=>'title',

                        'type'=>'raw',

                        'value'=>'$data->getMyValue()',


                ),




Secondly, you can try it as




                array(

                        'name'=>'title',

                        'type'=>'raw',

                        'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'.' '.'$data->create_time',

                ),




hi PeRoChAk,

thanks for your reply.it works fine as you say:


 array(

                        'name'=>'title',

                        'type'=>'raw',

                        'value'=>'$data->getMyValue()',


                ),

but if I use


 array(

                        'name'=>'title',

                        'type'=>'raw',

                        'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'.' '.'$data->create_time',

                ),

threw the exception:

Parse error: syntax error, unexpected ‘$data’ (T_VARIABLE) in D:\mypp\framework\base\CComponent.php(607) : eval()'d code on line 1

please see the code as below,and it works fine too:


		array(

			'name'=>'title',

			'type'=>'html',

			'value' => function($data,$row) {

                          $value =CHtml::link(CHtml::encode($data->title), $data->url).$data->create_time;

                           return $value;      

                         },

		), 

I would personally stick with the anonymous function as it’s much easier to read and maintain. If you really want to use the other form, try this:




'value'=>'CHtml::link(CHtml::encode($data->title), $data->url) . " " . $data->create_time',



I never use this, so the syntax may be wrong, but in essence, you should be doing the concatenation within the expression, or the PHP parser will see something nonsensical equivalent to this:




return CHtml::link(CHtml::encode($data->title), $data->url) $data->create_time;

//                                                          ^ Problem here



thanks.

anonymous function in view can only used in php V5.3,so we can use like this:

in view:




 array(

        'name'=>'title',

        'type'=>'raw',

        'value'=>array($this,'getTopStar'),

   ),



in Controller:




	public function getTopStar($data,$row){

	    $value =CHtml::link(CHtml::encode($data->title), $data->url);

		$ratstr='  ';

		for($i=1;$i<=$data->rating;$i++){

			$ratstr=$ratstr.'<img src="'.Yii::app()->request->baseUrl.'/images/bullet_star.png" />';

		}

	   return $value.$ratstr;	

	}



also,we can see the wiki reference:

cgridview-render-customized-complex-datacolumns