Hamid Ghorashi
Hamid Ghorashi

Reputation: 1001

Render a widget inside CGridView in Yii

I am using Yii framework and I have a problem. I have created a widget in extension folder and trying to render my widget inside a CGridView. The partial code (my grid) is like that:

'columns'=>array(
'id',
'name',
array(
    'type'  => 'raw',
    'value' =>  $this->widget('application.extensions.jalali.gregorian2jalali',array())
),

It gives me the following error while running:

Error 500
call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

What is the problem coming from?

Upvotes: 1

Views: 2688

Answers (4)

adriang
adriang

Reputation: 23


I was wondering the same and found out the following works for me:

    <?php $this->widget('bootstrap.widgets.TbGridView',array(
         'type'=>'striped bordered condensed',
         'id'=>'jugada-grid',
         'dataProvider'=>$model->search(),
         'filter'=>$model,
         'columns'=>array(
            'id',
            'nombre',
            'descripcion',
            array(
                    'name'  => 'animacion',
                    'header'=> 'Animación',
                    'type' => 'raw',
                    'value'=> "Yii::app()->controller->widget('bootstrap.widgets.TbButton', array(
                    'label'=>'Campo',
                    'type'=>'primary',
                    'htmlOptions'=>array(
                        'id' => 'activate-field',
                        'data-toggle'=>'modal',
                        'data-target'=>'#field-popup',
                    ),
                ),true)",
            ),
            array(
                'class'=>'bootstrap.widgets.TbButtonColumn',
            ),
    ),
)); ?>

Note that the example is based on widgets of the Yii Bootstrap extension.
Note the Yii::app()->controller->widget... wrapped in ".

Upvotes: 2

jmarkmurphy
jmarkmurphy

Reputation: 11473

value is specified incorrectly, it needs to be a string like so:

'value' =>  '$this->grid->controller
     ->widget("ext.jalali.gregorian2jalali",array(),true)'

Upvotes: 3

Truongnq
Truongnq

Reputation: 367

Alternative render widget in GridView and CGridColumn Try to push the widget into a function like

e.g: in Post model (Post.php file), create a new function named gregorian2jalali()

public static function gregorian2jalali(){
 return $this->widget('application.extensions.jalali.gregorian2jalali',array());
}

then in the view:

'columns'=>array(
'id',
'name',
array(
    'type'  => 'raw',
    'value' =>  "Post::gregorian2jalali()"
),

Upvotes: 2

Developerium
Developerium

Reputation: 7265

When a column is specified as an array, it will be used to create a grid column instance, where the 'class' element specifies the column class name (defaults to CDataColumn if absent). Currently, these official column classes are provided: CDataColumn, CLinkColumn, CButtonColumn and CCheckBoxColumn.

http://www.yiiframework.com/doc/api/1.1/CGridView#columns-detail

and value in CDataColumn : a PHP expression that will be evaluated for every data cell using evaluateExpression and whose result will be rendered as the content of the data cell. In this expression, you can use the following variables: $row the row number (zero-based). $data the data model for the row. $this the column object. A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.

just copy pasted it!

Upvotes: 0

Related Questions