Yii Framework Forum: How To Merge Two Column Into One Column Of Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Merge Two Column Into One Column Of Cgridview Rate Topic: -----

#1 User is offline   jbas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 04-October 12

Posted 22 January 2013 - 09:34 PM

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
0

#2 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 579
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 22 January 2013 - 09:55 PM

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


Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
0

#3 User is offline   jbas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 04-October 12

Posted 22 January 2013 - 11:27 PM

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;      
                         },
		), 

0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 23 January 2013 - 05:07 AM

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

0

#5 User is offline   jbas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 04-October 12

Posted 23 January 2013 - 09:27 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users