Adding item to CGridView

Hi, I’m new to yii and web programming.

I have a page set up like this :

-CGridView with a column containing the name of the item and another containing an ‘add’ button.

-Another CGridView, empty when you load the page

What I want to do is, when you click on the add button, the element next to the button is added to the second CGridView. How do I do that ? The items listed in the first CGridView are from a model, so I figured I should write a method in the controller linked to the said model. This is what I wrote in the controller file. (I’m kinda new to the MVC thing too.)

public function actionView($id)


	$model = $this->loadModel($id);

	if (yii::app()->request->isAjaxRequest)


	echo CJavaScript::jsonEncode(array(

		'id' => $model->id,

	        'nom' => $model->nom,

		'critere_id' => $model->critere_id,





	throw new CHttpException(400,'Invalid request. This probably occured because you do not have Javascript enabled. Please enable Javascript!');


If I get it right, this sends Javascript data containing information about the clicked element.

But how do I get it to be displayed in the separate CGridView ?

Also, if I want all items of a CGridView stocked into an array, how can I do that ?

Any help is much appreciated, thanks :)

First thing is to make sure, if you only want to display data in the next CGridView per current time or it you want to make this change permanent - i.e. change database, so any next time someone displays page, will see thing (things) added to the second CGridView?

If this is only first option, then you can use jQuery (or even pure JavaScript) to put whatever returns from your AJAX call to the second CGridView.

If this is second option, then I would suggest considering switching from AJAX call to normal page load / GET call. As contents of second CGridView must be reloaded after updating it with AJAX. Of course, this can also be done with AJAX-only - i.e. updated contents and reload CGridView only, without reloading whole page. But in my humble opinion it is much easier to do a normal GET call, update database and then - after reloading whole page - have contents of second (or both) CGridView properly updated.