Use CJuiDialog to create a new model

Source: Internet
Author: User
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 ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.