Using variables in onclick in CGridView

I’m trying to update 2 fields from a CJuiDialog using an onclick button. Using just the $data->id it works, however when I replace it with $data->rate($date)[“test”] it fails - even though data->rate($date)[“test”] works in the comparison immediately prior to passing the variable to the onclick.

Works:


$this->widget('zii.widgets.grid.CGridView', array(

		'id'=>'item-grid',

		'dataProvider'=>$items->search(),

		'filter'=>$items,

		'columns'=>array(

			...

			 array(

				  'header'=>'',

				  'type'=>'raw',

				  'value'=>'(!($data->rate("'.$date.'")["test"] == 0)) ? CHtml::Button("+", 

								array("name" => "send_item", 

									 "id" => "send_item", 

									 "onClick" => "$(\"#item_dialog\").dialog(\"close\"); $(\"#item_id\").val(\"$data->id\"); 

									 $(\"#Items_description\").val(\"$data->description\"); 

									 $(\"#Items_unit_price\").val(\"$data->id\");"

								)

							)

							: ""',

			),



Fails with error: Property "Item.rate" is not defined.


$this->widget('zii.widgets.grid.CGridView', array(

		'id'=>'item-grid',

		'dataProvider'=>$items->search(),

		'filter'=>$items,

		'columns'=>array(

			...

			 array(

				  'header'=>'',

				  'type'=>'raw',

				  'value'=>'(!($data->rate("'.$date.'")["test"] == 0)) ? CHtml::Button("+", 

								array("name" => "send_item", 

									 "id" => "send_item", 

									 "onClick" => "$(\"#item_dialog\").dialog(\"close\"); $(\"#item_id\").val(\"$data->id\"); 

									 $(\"#Items_description\").val(\"$data->description\"); 

									 $(\"#Items_unit_price\").val(\"$data->rate(\"'.$date.'\")[\"test\"]\");"

								)

							)

							: ""',

			),



Any idea how I can use $data->rate($date)["test"] in this onclick?

Try using closure that keeps text more readable:




<?php

    $this->widget('zii.widgets.grid.CGridView', array(

                'id'=>'item-grid',

                'dataProvider'=>$items->search(),

                'filter'=>$items,

                'columns'=>array(

                        ...

                         array(

                                  'header'=>'',

                                  'type'=>'raw',

                                  'value' => function($data) use($date)

                                  {

                                    if(!($data->rate($date)["test"] == 0)){

                                        return CHtml::Button("+", 

                                                                array("name" => "send_item", 

                                                                         "id" => "send_item", 

                                                                         "onClick" => "$('#item_dialog').dialog('close'); $('#item_id').val('".$data->id."'); 

                                                                         $('#Items_description').val('".$data->description."'); 

                                                                         $('#Items_unit_price').val('".$data->rate($date)["test"]."');"

                                                                )

                                                        )

                                    }

                                    else

                                    {

                                        return "";

                                    }

                                      

                                      

                                  }

                        ),

?>



Wow, thanks a lot, works great! Just missing a semicolon:


<?php

    $this->widget('zii.widgets.grid.CGridView', array(

                'id'=>'item-grid',

                'dataProvider'=>$items->search(),

                'filter'=>$items,

                'columns'=>array(

                        ...

                         array(

                                  'header'=>'',

                                  'type'=>'raw',

                                  'value' => function($data) use($date)

                                  {

                                    if(!($data->rate($date)["test"] == 0)){

                                        return CHtml::Button("+", 

                                                                array("name" => "send_item", 

                                                                         "id" => "send_item", 

                                                                         "onClick" => "$('#item_dialog').dialog('close'); $('#item_id').val('".$data->id."'); 

                                                                         $('#Items_description').val('".$data->description."'); 

                                                                         $('#Items_unit_price').val('".$data->rate($date)["test"]."');"

                                                                )

                                                        );

                                    }

                                    else

                                    {

                                        return "";

                                    }

                                      

                                      

                                  }

                        ),

?>

Hi

As I suspect ‘description’ to have to have ‘free format’ content, I suggest a small improvement like this:


 "$('#Items_description').val(".CJavaScript::encode($data->description).");"

This will ensure that quoting and escaping is done appropriately for the description field.