CGridView

Is there a way to include dataProvider’s property into button’s onclick action?

For example I have a property id and the next code




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

		'dataProvider'=>$dataProvider,

		'columns'=>array(

		'id',

		'lesson_title',

				array(

						'class'=>'CButtonColumn',

						'template' => '{up}',

						'buttons'=>array

						(

								'up' => array

								(

										'label'=>'+',

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

										'click'=>"function(){

										alert(id);

										}",

								),

						)

				)

		)));

?>



How could I get the row id property into the alert function?

Thanks


$row->id

?

The ‘click’ property is not treated as an expression, instead you will need to be a little more creative. If you are not using the ‘url’ property, you could put the id in that, read it out with jQuery and show that.




'buttons'=>array(

  'up' => array(

    'label'=>'+',

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

    'url'=>'$row', //you can also use $data->attribute (where attribute is the model attribute you want)

    'click'=>"function(){

      alert($(this).attr("href"));

      return false; //this is important or it will navigate to the url which is garbage

    }",

),



You’re totally right! I forgot that.

Here’s something cool :D


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

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        'id',

        'lesson_title',

        array(

            'class'=>'CButtonColumn',

            'template' => '{up}',

            'buttons'=>array

            (

                'up' => array

                (

                    'label'=>'+',

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

                    'click'=>"function(){

                        alert($('.keys span').eq($('table.items tbody td.button-column a').index($(this))).html());

                    }",

                ),

            )

        )

    ),

));

Thanks for the suggestions. I was thinking that maybe I could create a button to contain onclick inside, using CGridView, like I would if I would parse all the dataProvider rows




<script>

function display($id)

{

alert($id);

return false;

}

</script>


<table>


<?php foreach($results as $result):?>

<tr>

<td></td>

....

<td></td>

<td>

<input type="button" onclick="display(<?php echo $result->id?>)" />

</td>

</tr>


<?php  endforeach; ?>