jbas
(Jbas)
January 23, 2013, 2:34am
1
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
perochak
(Amjad Mughal)
January 23, 2013, 2:55am
2
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',
),
jbas
(Jbas)
January 23, 2013, 4:27am
3
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;
},
),
Keith
(Kburton)
January 23, 2013, 10:07am
4
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
jbas
(Jbas)
January 24, 2013, 2:27am
5
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