Question on "cjuidialog for create new model" tutorial

Hello,

I am very new to Yii and php, I really like how this community works together and has great documentation.

I just implemented Zaccaria’s tutorial on cjuidialog for create @ http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/ and I was wondering how I can make the original page or field update trough ajax so that the just added item is reflected in the field or in the list?

Thank you in advance.

Your problem can be splitted in 2 parts:

  • Return the new dropDownList

  • Place the dropDownList in the right place in the page.

A very simple method is this one:

Let’s put a div among the ddl, just for make the stuff easy. In your view you will have:





<div class="row">

	<?php echo $form->labelEx($model,'field'); ?>

	<div id="divForDddl"><?php echo $form->dropDownList($model,'field', array(...)); ?></div>

	<?php echo $form->error($model,'field'); ?>

</div>



Just a div more.

Now, in case of success, in the controller we will return a new dropDownList, with selected the new value:




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

{

	$mainModel = new MyMainModel;

	$mainModel->field= $model->primaryKey; // let's select the newly created value

	echo CJSON::encode(array(

		'status'=>'success', 

		'ddl'=>CHtml::ActiveDropDownList($model, 'field', array(...)) // a ddl with the value already selected!

		));

	exit;               

}




This should send the whole dropdownList, with the new value in list (because we generate the ddl after saving the model) and with the value selected (we set this value in the model before generate the ddl).

Now we have to change the js as follows:




	if (data.status == 'failure')

	{

		$('#dialogClass div.divForForm').html(data.div);

			  // Here is the trick: on submit-> once again this function!

		$('#dialogClass div.divForForm form').submit(addClass);

	}

	else

	{

		$('#divForDddl').html(data.ddl);

	}




In case of success, we replace the content of the divForDddl (that contains only a ddl) with the ddl we sent.

I didn’t tested anything, so be careful!

P.S: welcome to the forum

Thank you for the quick reply.

My main form is called Player and I am calling the add Team form in the dialog. Does the controller code go in the controler for Player or Team?

and if it goes in the controler for Team doe I replace the initial code or add it? Could you maybe add an example in the form of the tutorial.

I think I am missing something here.

Greetz

The controller code should be in team controller, it should edit the team/create.

The team action sould be something like:





public function actionCreate()

{

	$model=new Team;


	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model);


	if(isset($_POST['Team']))

	{

		$model->attributes=$_POST['Team'];

		if($model->save())

		{

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

			{

					$player = new Player ;

					$player->team= $model->primaryKey; // let's select the newly created value

					echo CJSON::encode(array(

							'status'=>'success', 

							'ddl'=>CHtml::ActiveDropDownList($model, 'team', array(...)) // a ddl with the value already selected!

							));

					exit;               

			}


			else

				$this->redirect(array('view','id'=>$model->id));

		}

	}


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

	{

		echo CJSON::encode(array(

			'status'=>'failure', 

			'div'=>$this->renderPartial('_form', array('model'=>$model), true)));

		exit;               

	}

	else

		$this->render('create',array('model'=>$model,));

}




Didn’t get it to work yet but i’ll spend some time on it this week.

Many Thanks so far.