I am using a CGridView with CActiveDataProvider. Everything works fine however now I have a need to show a custom button depending upon the id of the row.
I can add the button and get it to work as well however I cannot customise the image according to the row id. I can see from examples that when creating url I can access the id of row by calling $data->id however when I try to do that for imageURL it does not work. My code is below:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'desc',
'created',
array(
'class'=>'CButtonColumn',
'header'=>'Active State',
'template'=>'{rank}',
'buttons'=>array(
'rank'=>array(
'label'=>'Rank',
'imageUrl'=>$this->getRank($data->id),
'url'=>'Yii::app()->createUrl("users/updateRank", array("id"=>$data->id))',
'options'=>array(
'ajax'=>array(
'type'=>'POST',
'url'=>"js:$(this).attr('href')",
),
),
),
),
),
),
));
Image URL calls a function in my controller which returns a url to the image. However I cannot pass the id of the current record user is clicking upon and I always get the default image back. If I use $data->id I get an error that data does not exist. If I use $this->id it is referring to the controller and I get the controller id. Anyway I can get the id of the row and pass it over to a function.
It seems you're missing single quotes:
is a step forward (probably you need to do something more to get your custom function work this way)
In CGridView, any php code that you want to run independently for each row, must be in quotes - if you use 'naked' php, it would be calculated only once and then the value will be same for each row.