need help with CButtonColumn of grid

I have a problem with CButtonColumn of grid, I need to store an ID contact in PHP variable, to use the variable in a Javascript function, is very important. Help please…

the code:




array

                (

                    'class'=>'CButtonColumn',

                    'header'=>'Acciones',

                    'template'=>'{Detail}{Commentary}{Address}{CompanyEmployee}{Quote}{Task}',

                    'buttons'=>array

                    (

                        'Detail' => array

                        (

                            'label'=>'Información del Contacto',

                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/eye.png',

                            'url'=>'Yii::app()->createUrl("contact/viewWindow",array("id" => $data["id_contact"]))',

                            'click'=>'js:function(){

                                var aux=$data->id_contact; //here is the problem!!!!

                                $.window({

                                       title: var, //here is the problem!!!!

                                       url: $(this).attr("href"),

                                       resizable:true,

                                       height:600,

                                       width:1020,

                                       maxWidth:1020,

                                       bookmarkable:false,

                                       showFooter:false,

                                       onClose: function(){

                                                $.fn.yiiGridView.update("Contact1"),

                                                $.fn.yiiGridView.update("Contact2")

                                               }

                                    });

                                    $.window.prepare({

                                        dock:"bottom",

                                        showLog:true

                                    });

                                    return false;

                            }'

                        ),



var aux=$data->id_contact; //here is the problem!!!!

this line is js code ,will be output without any change ,so $data is invalidate ! you must want to trans $data->id_contact to js variable aux .

actually your problem is not that easy thing . here i give you my thought ,but not test :

you should read the doc CButtonColumn first .

if you want refer $data->id_contact you must save it somewhere and evaluate it first (you see $data is php variable ) , then use jquery to fetch it . may be you can save it in some other dataCell but is invisible:

    <span style="display:none">here your data<span>       

see CDataColumn::value attribute . because above html is only plan string so you can use php expression :




  CHtml::tag("span",array("style"=>"display:none"),$data["id_contact"]).$data["someOhterColumnName"] 


  //above code will be evaluate first and as value of some data cell . then you can use jquery to fetch it 

  //  this invisible span and you  "js this" has some sibling relation in dom tree.



another method :

extends CButtonColumn ,rewrite the renderButton method




   class MyButtonColumn extends CButtonColumn{

    

   /**

   * here add an variable to accept option expression

   *   array( "someKey"=>$data["soveColumnName"])

   */

   public $optionsExpression;


   protected function renderButton($id,$button,$row,$data)

        {

                if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))

                        return;

                $label=isset($button['label']) ? $button['label'] : $id;

                $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';

                $options=isset($button['options']) ? $button['options'] : array();


                //here  is the magic


                if(!empty($this->optionsExpression)){

                    

                $options = CMap::mergeArray($options,$this->evaluateExpression(

$this->optionsExpression,array('data'=>$data,'row'=>$row)));

                    

                }

                

                if(!isset($options['title']))

                        $options['title']=$label;

                if(isset($button['imageUrl']) && is_string($button['imageUrl']))

                        echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);

                else

                        echo CHtml::link($label,$url,$options);

        }

  }

  // you see you can pass $data['someAttr'] as html link element's attribute and then use  jquery $(this).attr('myAttrName') to access it  




this just my thought for your problem , i’v not tested it . just for your referring .

yiqing95,

thank you very much, i will review the code… is much level for my, but I will strive :D