Yii Framework Forum: Passing variable inside CButtonColumn inside CGridView - Yii Framework Forum

Jump to content

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

Passing variable inside CButtonColumn inside CGridView Rate Topic: -----

#1 User is offline   akamine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-June 11

Posted 16 June 2011 - 08:09 AM

Hello, I'm new in Yii.

I have a problem with CButtonColumn inside CGridView,

what I want to achieve is generating dynamic onClick handler when a button in CButtonColumn is clicked.

Here's what I'e tried and fail:
this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'roomTypeForm-grid',
		'dataProvider'=>$model->search(),
		'filter'=>$model,
		'columns'=>array(
                    //name button
                    'name',
                    array(
                        'class'=>'CButtonColumn',
                        'template'=>'{update}{delete}',
                        'buttons' => array(
                            'update' => array(
                                'url' => '"#"',
                                'click' => '
                                    function(){
                                    openForUpdate($data->id,$data->name,function(){ alert("updated"); $.fn.yiiGridView.update("roomTypeForm-grid"); }); // << THIS IS WHAT I WANT
                                    }
                                ',
                            ),
                        ),
                    ),
		),
	));


the problem is, this line:
                                'click' => '
                                    function(){
                                    openForUpdate($data->id,$data->name,function(){ alert("updated"); $.fn.yiiGridView.update("roomTypeForm-grid"); }); // << THIS IS WHAT I WANT
                                    }
                                ',


basically, the $data->id can't be passed into the click event..

Is there any work around this?
0

#2 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 640
  • Joined: 23-October 10
  • Location:Romania

Posted 16 June 2011 - 08:53 AM

Have you tried:
'click' => 'js:openForUpdate("."$data->id".","."$data->name".",function(){
       alert("updated"); 
       $.fn.yiiGridView.update("roomTypeForm-grid");
       }',


L.E: Not sure about concatenation of the php vars in the eval() mode for this case, maybe you need to tweak them a bit.
0

#3 User is offline   jwerner 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 04-December 08
  • Location:Germany

Posted 16 June 2011 - 09:13 AM

I found this works:
array(
	'class'=>'CButtonColumn',
            	'template'=>'{test}',
            	'buttons'=>array(
                		'test'=>array(
                    			'label'=>'AAA',
                    			'url'=>'"#"',
                    			'click'=>'function(){var rowIdx=$(this).closest("tr").attr("sectionRowIndex");var rowId=$("#"+$(this).closest("div.grid-view").attr("id")+" > div.keys > span:eq("+rowIdx+")").text();alert(rowId);}', 
                		),
	)
        ),


Regards,

Joachim
0

#4 User is offline   akamine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-June 11

Posted 16 June 2011 - 10:18 AM

View Postjwerner, on 16 June 2011 - 09:13 AM, said:

I found this works:
array(
	'class'=>'CButtonColumn',
            	'template'=>'{test}',
            	'buttons'=>array(
                		'test'=>array(
                    			'label'=>'AAA',
                    			'url'=>'"#"',
                    			'click'=>'function(){var rowIdx=$(this).closest("tr").attr("sectionRowIndex");var rowId=$("#"+$(this).closest("div.grid-view").attr("id")+" > div.keys > span:eq("+rowIdx+")").text();alert(rowId);}', 
                		),
	)
        ),


Regards,

Joachim


Wow, that works! :)

I just learned jquery, so I have difficulty understanding your code. would you please explain what you did there?
0

#5 User is offline   akamine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-June 11

Posted 16 June 2011 - 07:53 PM

I also need to get the name (another field in the record), how do I do that?
0

#6 User is offline   jwerner 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 04-December 08
  • Location:Germany

Posted 17 June 2011 - 01:26 AM

View Postakamine, on 16 June 2011 - 10:18 AM, said:

Wow, that works! :)

I just learned jquery, so I have difficulty understanding your code. would you please explain what you did there?


// $(this) is the button element
// Get the closest/nearest TR / table row, and of that row, get the value of its index attribute (i.e. the 0-based row number):
var rowIdx=$(this).closest("tr").attr("sectionRowIndex");

// "#"+$(this).closest("div.grid-view").attr("id") will get the id of the grid view div
// Inside the grid view, there is a div with the class 'keys', where the primary keys are stored inside span tags
// The order of the prim. key spans corresponds to the table rows:
var rowId=$("#"+$(this).closest("div.grid-view").attr("id")+" > div.keys > span:eq("+rowIdx+")").text();
alert(rowId);


Regards,

Joachim
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