This article describes how to render custom datacolumn: CGridView: Render customized/complex datacolumns
Using the example with calling the method from controller we end up with this:
view:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array(
'name'=>'newColumn',
//call the method 'gridDataColumn' from the controller
'value'=>array($this,'gridDataColumn'),
),
),
));
controller
class MyController extends Controller
{
...
protected function gridDataColumn($data,$row)
{
...
}
...
}
Works fine placing the widget inside a view directly related to MyController instance (because of $this in ‘value’ array).
Now using the same widget in another view (that is not related to MyController) I have to do something like this to make things work:
Yii::import('application.controllers.MyController');
$controller=new MyController('index');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array(
'name'=>'newColumn',
//call the method 'gridDataColumn' from the controller
'value'=>array($controller,'gridDataColumn'),
),
),
));
Works, but seems a little bit "patched". Is there a more elegant way to do this?