How get the id of a row in CGridView?

Hi all, i’m trying edit de buttons in CGridView(edit,delete,view) and when you click over this(edit,delete,view) i need launch a js script using the current id of de “row”, i try get the value using $data->id but isn’ts work (sorry for my english is so bad…)

here some of code of the view




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

    'id' => 'vehicle-grid',

    'htmlOptions' => array(),

    'cssFile' => false,

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

    'filter' => $model,

    'columns' => array(


....




array(

            'class' => 'CButtonColumn',

           

            'buttons'=>array(

                'update' => array(

                    'label'=>'Editar',     // text label of the button

                    'url'=>'Yii::app()->createUrl("vehicle/update",array("id"=>$data["id"]))',// a PHP expression for generating the URL of the button

                    

                    'options'=>array('onClick'=>'openUpdateDialog();return false;'),     // a JS function to be invoked when the button is clicked i need pass the id on "openUpdateDialgo('id')"

                    

                ),

                 'template'=>'{update}',

            ),



what is wrong??, how I can fix this??

thks…

this is an object




'dataProvider' => $model->search()



this is an array




$data["id"]



maybe you need do something like this




$data->id



You could try this:


'url'=>'Yii::app()->urlManager->createUrl("vehicle/update",array("id"=>$data->id))

Hi,

I have the same question, but $data->id still doesn’t working:

In CGridView’s ‘column’:




array(            

				'name' => '',

				'type'=>'raw',

				'header' => 'TestHeader',

				'value'=>'CHtml::label("", null, array("id"=>"wow_".$data->id))',

				'htmlOptions'=>array("onClick"=>"aa('".$data->id."')"),

			),



And the output:

<td onClick=“aa(’’)”>

The id is missing. (but label’s id is OK)

How can I fix this?

Thank you for any help,

Canadi

Change your line from this:

array(“onClick”=>“aa(’”.$data->id."’)"),

to this:

array(

&quot;onClick&quot;=&gt;'aa(&quot;&#036;data-&gt;id&quot;)', //maybe &quot;aa('&#036;data-&gt;id')&quot; ),

(Look at the quotes.)

Hi,

thank you for your answer but I think I have tried all existing quote modes in the past few days.

Results:


'htmlOptions'=>array("onClick"=>'aa("$data->id")'),

Output: <td onclick="aa("$data->id")">


'htmlOptions'=>array("onClick"=>"aa('$data->id')"),

Output: <td onclick=“aa(’’)”>

I really don’t know what is the solution. Is there any other way to make a cell clickable with model’s id parameter?

Canadi

Why don’t you use type ‘html’ instead of ‘raw’ in this case?

Because it absolutely doesn’t matter. Wheter ‘type’ is raw or html, it doesn’t working…

Currently, the following types are recognizable:

  • raw: the attribute value will not be changed at all.
  • text: the attribute value will be HTML-encoded when rendering.
  • html: the attribute value will be purified and then returned.

That was from documentation.

Then, I guess you have not had a value in your id.

For example, if u do ‘$m=new Model’ and try ‘$m->id’ you have not an ID in this point. After ‘$m->save()’ you will be able to use $m->id.

Have you checked this out?

The htmlOptions cannot access $data object with column values - therefore suggested solution above cannot work. Getting row ID requires a little trick, here’s what worked for me:

  1. add some hidden field named e.g. "rowid" into grid column:

'columns'=>array(


	array(

		'name' => 'adate',

		'value' => '$data->adate."<input type=\"hidden\" name=\"rowid\" value=\"".$data->id."\" />"',

		'type' => 'raw',

	),


        // .. more columns ...

)

  1. define htmlOption for your button with the onclick attribute (dont use "click" option because if rendering page via ajax, it will attach event handler multiple times)

'buttons' => array(


	'info' => array(

		'label' => 'Detail',

		'options'=>array('onclick'=>'var id=$(this).parent().parent().parent().find("input[name=rowid]").val();window.myPlugin.loadDetail(id);'),

	),

)

Try $data->primaryKey.

It works on TbExtendedGridView. Maybe it works on CGridView as well.