Render a widget inside CGridView in Yii

2019-08-13 09:46发布

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?

标签: php yii widget
4条回答
叼着烟拽天下
2楼-- · 2019-08-13 10:15


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 ".

查看更多
Deceive 欺骗
3楼-- · 2019-08-13 10:20

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!

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-13 10:27

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

'value' =>  '$this->grid->controller
     ->widget("ext.jalali.gregorian2jalali",array(),true)'
查看更多
Luminary・发光体
5楼-- · 2019-08-13 10:36

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()"
),
查看更多
登录 后发表回答