CGridView how to select the id of the selected row?

Hello , it’s me again… ^^

still got trouble, I have another problem. I don’t know how to select the id of the selected row of a table. Does anybody know how to do it?

i have tables

functional_domain

pk->FUNCTIONAL_DOMAIN_ID

is_owned_by

pk->IS_OWNED_BY_ID (auto_incrément)

unique key->(FUNCTIONAL_DOMAIN_ID, BA_ID)

business_analyst

pk->BA_ID

I have a CGridView in my functional_domain/views showing the table used to show the list of the link between the rows of two table

functional_domain<----is_owned_by---->business_analyst

this CGridView shows the business_analyst.BA_ID with

functional_domain.FUNCTIONAL_DOMAIN_ID = is_owned_by.FUNCTIONAL_DOMAIN_ID

business_analyst.BA_ID = is_owned_by.BA_ID

and it works perfectly

but, i would like to add a column in my CGridView, the CButtonColumn buttons , with only {view} and {delete}

here is what i did

functional_domain/view


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

                'id'=>'child-grid',

                'dataProvider'=>$business_analystRecords,

                'columns'=>array(

					'BA_ID',

					'BA_NAME',

					array(	

						'class'=>'CButtonColumn', 

							'template'=>'{view} {delete}',

							'deleteButtonUrl'=>'',

							'viewButtonUrl'=>'Yii::app()->controller->createUrl("isOwnedBy/view",array("id"=>"'.isOwnedBy::model()->findBySql( 'select IS_OWNED_BY_ID FROM is_owned_by where FUNCTIONAL_DOMAIN_ID="'.$model->FUNCTIONAL_DOMAIN_ID.'" AND BA_ID=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />')->IS_OWNED_BY_ID.'" ))',

					),

				),  				

)); 

functional_domain/controller


public function actionView($id)

	{

		$first_parent_instance = businessAnalyst::model()->find();

		$is_owned_byRecords= isOwnedBy::model()->find();	

		

		

		$this->render('view',array(

			'model'=>$this->loadModel($id),

			'business_analystRecords'=>$this->getBusinessAnalystRecords($id),

			'first_parent_instance'=>$first_parent_instance,

			'is_owned_byRecords'=>$is_owned_byRecords,


		));

	}

Hi,

if i undersand correctly your post, you could test this extension: http://www.yiiframework.com/extension/multimodelform

I use it and in my view form i can see the details of the main model (functional_domain in your case) and and grid of corresponding related model (is_owned_by for you).

My problem is here


'viewButtonUrl'=>'Yii::app()->controller->createUrl("isOwnedBy/view",array("id"=>"'.isOwnedBy::model()->findBySql( 'select IS_OWNED_BY_ID FROM is_owned_by where FUNCTIONAL_DOMAIN_ID="'.$model->FUNCTIONAL_DOMAIN_ID.'" AND BA_ID=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />')->IS_OWNED_BY_ID.'" ))',

i would like to get the BA_ID of the selected row to send it to my SQLquery and so be able to access to its view or delete it


'viewButtonUrl'=>'Yii::app()->controller->createUrl("isOwnedBy/view",array(

							"id"=>"'.isOwnedBy::model()->findBySql( 

								'SELECT IS_OWNED_BY_ID FROM is_owned_by 

								WHERE FUNCTIONAL_DOMAIN_ID="'.$model->FUNCTIONAL_DOMAIN_ID.'" 

								AND BA_ID= "<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />"'

							)->IS_OWNED_BY_ID.'" 

						))',

to make it easier to understand

i read that on CGridView , i think it would help me but i don’t really understant


/**

	 * @var string a javascript function that will be invoked after the row selection is changed.

	 * The function signature is <code>function(id)</code> where 'id' refers to the ID of the grid view.

	 * In this function, you may use <code>$.fn.yiiGridView.getSelection(id)</code> to get the key values

	 * of the currently selected rows.

	 * @see selectableRows

	 */

	public $selectionChanged;

i tried tu use this variable but i did not managed to

$.fn.yiiGridView.getSelection(id)

I’m not sure that your syntax is correct, but you could try $data->BA_ID

i tried to do this, but it says me $data doesn’t exist

I believe that you have to keep $data in the master value string in order to make Yii process it…

Anyway, try with this:


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

    'id'=>'child-grid',

    'dataProvider'=>$business_analystRecords,

    'columns'=>array(

        'BA_ID',

        'BA_NAME',

        array(  

            'class'=>'CButtonColumn', 

            'template'=>'{view} {delete}',

            'buttons'=>array (

                'view' => array(

                    'url'=>'array("isOwnedBy/view", "id"=>isOwnedBy::model()->findBySql("select IS_OWNED_BY_ID FROM is_owned_by where FUNCTIONAL_DOMAIN_ID=\'" . $model->FUNCTIONAL_DOMAIN_ID . "\' AND BA_ID=$data->BA_ID")->IS_OWNED_BY_ID)'

                )

            )

        )

    )

));

undefened variable model :(

Sorry a quote error. $model should not be parsed and must be outside the string, like this:


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

    'id'=>'child-grid',

    'dataProvider'=>$business_analystRecords,

    'columns'=>array(

        'BA_ID',

        'BA_NAME',

        array(  

            'class'=>'CButtonColumn', 

            'template'=>'{view} {delete}',

            'buttons'=>array (

                'view' => array(

                    'url'=>'array("isOwnedBy/view", "id"=>isOwnedBy::model()->findBySql("select IS_OWNED_BY_ID FROM is_owned_by where FUNCTIONAL_DOMAIN_ID=\'' . $model->FUNCTIONAL_DOMAIN_ID . '\' AND BA_ID=$data->BA_ID")->IS_OWNED_BY_ID)'

                )

            )

        )

    )

));

found it!!! with this syntax :D


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

                'id'=>'child-grid',

                'dataProvider'=>$business_analystRecords,

                'columns'=>array(

					'BA_ID',

					'BA_NAME',

					array(	

						'class'=>'CButtonColumn', 

						'template'=>'{view} {delete}',

						'viewButtonUrl'=>'Yii::app()->controller->createUrl("isOwnedBy/view",array(

							"id"=>isOwnedBy::model()->findBySql( 

								"SELECT IS_OWNED_BY_ID FROM is_owned_by 

								WHERE FUNCTIONAL_DOMAIN_ID= '.$model->FUNCTIONAL_DOMAIN_ID.'

								AND BA_ID=\'$data->BA_ID\'

							")->IS_OWNED_BY_ID

						))',

						'deleteButtonUrl'=>'Yii::app()->controller->createUrl("isOwnedBy/delete",array(

							"id"=>isOwnedBy::model()->findBySql( 

								"SELECT IS_OWNED_BY_ID FROM is_owned_by 

								WHERE FUNCTIONAL_DOMAIN_ID= '.$model->FUNCTIONAL_DOMAIN_ID.'

								AND BA_ID=\'$data->BA_ID\'

							")->IS_OWNED_BY_ID

						))',

					),

				),  				

)); 

thanks everyone ;)