In this tutorial, we will learn how to use a dialog box to create an interface. Here is a similar tutorial, implemented using Ajax, but we will use a simpler and more effective method: onSubmit method of the form
In this tutorial, we will learn how to use a dialog box to create an interface.
Here is a similar tutorial, implemented using Ajax, but we will use a simpler and more effective method: form onSubmit method
Solution
Suppose we have a classroom with many students. If the user fills in the student information in the form but does not have the student's classroom, he should create the classroom, but he will lose the input just now.
We want to allow users to create classrooms on the student list page without changing the page.
Prepare a form
First, we need a form for creating a classroom. We can use gii to generate the CRUD code we need.
Once we are satisfied with our form and it can work when submitting the form, we can use it in the dialog box.
Enhance the create action
We need to enhance the create action of the classroom Controller.
Let's use the following method:
public function actionCreate() { $model=new Classroom; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Classroom'])) { $model->attributes=$_POST['Classroom']; if($model->save()) { if (Yii::app()->request->isAjaxRequest) { echo CJSON::encode(array( 'status'=>'success', 'div'=>"Classroom successfully added" )); 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,)); }
We added some minor changes: in the ajax request, we wrote a json-encoded array.
Dialog box
Now that the background is complete, let's write the dialog box
Add the following code somewhere in the student form:
'Cursor: pointer; text-decoration: underline; ', 'onclick' => "{addClassroom (); $ (' # dialogClassroom '). dialog ('open') ;}") ;?>
BeginWidget ('zii. widgets. jui. CJuiDialog ', array (// the dialog 'id' => 'dialogclassroom', 'options' => array ('title' => 'Create classroom ', 'autoopen' => false, 'modal' => true, 'width' => 550, 'height' => 470,),);?>
EndWidget ();?>