Yii Framework Forum: Problem With Cgridview And Anonymous Function - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem With Cgridview And Anonymous Function Yii 1.1.13 Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 726
  • Joined: 02-February 09

Posted 29 January 2013 - 08:16 AM

I have the following code:


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'post-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
                array(
                    'name'=>'title',
                    'value'=>'function(){ print "op" }',
                ),
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>


The result is "op" is printed on the top of the page instead of in the proper cell. Am I using something wrong. It becomes even worse if I am using a static method such as:

'value'=>'Post::render',


Any ideas on what I am doing wrong here?

cheers!
0

#2 User is offline   mbala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 21-July 12
  • Location:Chennai, Tamilnadu, India

Posted 29 January 2013 - 08:25 AM

    array(
                    'name'=>'title',
                    'value'=>'op',
                ),

if you want to display value from function, Create one function in current model and call using below code
    array(
                    'name'=>'title',
                    'value'=>'$data->functionname($data->title)',
                ),

0

#3 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 726
  • Joined: 02-February 09

Posted 29 January 2013 - 08:38 AM

View Postmbala, on 29 January 2013 - 08:25 AM, said:

    array(
                    'name'=>'title',
                    'value'=>'op',
                ),

if you want to display value from function, Create one function in current model and call using below code
    array(
                    'name'=>'title',
                    'value'=>'$data->functionname($data->title)',
                ),




Thanks. I already knew how to call a method from the model. But in the manual it says:

http://www.yiiframew...pression-detail

Quote

Valid PHP callback can be class method name in the form of array(ClassName/Object, MethodName), or anonymous function (only available in PHP 5.3.0 or above).


I would like to see how to call an anonymous function as I am using php > 5.3 :)
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 January 2013 - 08:53 AM

Dear Friend

If you put the value in single quotes, CComponent::evaluateExpression will evaluate the expresssion.

If you pass without quotes, it will call the anonymous function.

The anonymous function should have proper signature.

array(
		'header'=>"Test",
		'value'=>function($data,$row,$object) {
                           echo $data->title;
                           echo $row;
                           echo $object->id;

                             },
		),


The parameters should be in the in the same order mentioned.

$data represents the data object representing current row.
$row is 0 based index numbering rows.
$object is the component itself,here it is instance of CDataColumn.

The same way class methods and methods of object can be passed.

Regards.
1

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 726
  • Joined: 02-February 09

Posted 29 January 2013 - 09:00 AM

thanks seenivasan.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users