/****
CJuiDialog for create new model http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/ translated by php攻城师 http://blog.csdn.net/phpgcs Introduction
Scenario
Preparation of the form
Enhance the action create
The dialog
Summary
***/ Introduction 本教程关于如何 用一个对话框实现一个新建界面
这有点类似 使用 Ajax 链接来实现目的, 但是我们将会是哟你个一个更简单和更高效的方式:
表单的onSubmin 事件(the event onSubmit of the form) 背景 Scenario 假设我们有一个很多学生的教室。 在没有创建教室的情况下, 如果用户想要填写学生信息的表单, 需要先创建一个教室 并且会丢失掉之前已经输入的学生信息。。。
我们想要允许用户从学生表单中创建教室,而不需要更改页面。 准备表单 Preparation of the form 首先,我们要 一个创建教室的表单。 我们可以用 gii 来生成一个 crud 。。
在 普通提交的情况下,这个表单工作正常了以后, 我们可以将其用于一个 对话框。 增强 创建动作 Enhance the action create
我们需要 增强 创建教室的控制器动作, 如下: 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,));
} 我们做了一些小改动:
在ajax 请求的情况下 我们写了一个 json 编码的数组。在这个数组中, 我们返回了一个状态 , 整个表单使用 renderPartial 来创建的。 现在后台已经完成,我们来写对话框。 <?php echo CHtml::link('Create classroom', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; text-decoration: underline;',
'onclick'=>"{addClassroom(); $('#dialogClassroom').dialog('open');}"));?> <?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogClassroom',
'options'=>array(
'title'=>'Create classroom',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>470,
),
));?> <div class="divForForm"></div> <?php $this->endWidget();?> <script type="text/javascript">
// here is the magic
function addClassroom()
{
<?php echo CHtml::ajax(array(
'url'=>array('classroom/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogClassroom div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogClassroom div.divForForm form').submit(addClassroom);
}
else
{
$('#dialogClassroom div.divForForm').html(data.div);
setTimeout(\"$('#dialogClassroom').dialog('close') \",3000);
} } ",
))?>;
return false; } </script> 就这些, 这些代码我都做了些什么? 1, 一个链接,用来打开对话框
2, 对话框本身, 其中是一个 将会被 ajax 替代的 div
3, js 函数 addClassroom()
4, 这个函数出发了一个ajax 请求, 执行了我们在前面步骤中 准备的 create classroom 的动作。
5, 在 status failure 的情况下, 返回的 form 将会 在 对话框中
在 status success 的情况下, 我们将 替换 div 并在3秒后 关闭对话框 你还可以返回 新插入的 classroom 的 id ,并将其植入 一个下拉列表 并自动选中。 总结: 准备常规的 gii 生成的 creation 动作代码
修改 create 动作 ,增加 处理Ajax 请求的代码
在任何地方,你可以防止 link , dialog , js 代码 此方法非常合适, 因为它changes anything in the code of the _form ,因此任何最终添加到 classroom 的 字段 都将在 标准的/对话框 的创建表单中 通用。

yii_wiki_145_yii-cjuidialog-for-create-new-model (通过CJuiDialog来创建新的Model)的更多相关文章

  1. laravel创建新model数据的两种方法

    laravel中的CRUD操作中,通过对代表数据表中row的model对象操作,来更新数据库表. 对于创建新的row的操作,有两种功能上相同的方法: 1.create: $user = User::c ...

  2. EF5&MVC4 学习1、创建新的Contoso University Application,并创建Model Class 生成对应的database

    参考:http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/creating-an-entity-framewo ...

  3. magento中Model创建以及该Model对于数据库的增删改查

    本文是按照magento英文文档照做与翻译的. Model层的实现是mvc框架的一个巨大的部分.它代表了你的应用的数据,或者说大多数应用没有数据是无用的.Magento的Model扮演着一个重要的角色 ...

  4. rails 创建项目、创建controller、model等

    rails2之前创建新项目: rails3以及更高版本创建新项目:rails new webname 创建数据表model:rails g model user name:string sex:str ...

  5. [Tensorflow] 使用 model.save_weights() 保存 / 加载 Keras Subclassed Model

    在 parameters.py 中,定义了各类参数. # training data directory TRAINING_DATA_DIR = './data/' # checkpoint dire ...

  6. Django 小实例S1 简易学生选课管理系统 3 创建用户模型(model)

    Django 小实例S1 简易学生选课管理系统 第3节--创建用户模型(model) 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 本文涉及到的新 ...

  7. php大力力 [023节]CREATE TABLE创建新表sql写字段备注(2015-08-27)

    2015-08-27 php大力力023.CREATE TABLE创建新表sql写字段备注 http://www.cnblogs.com/dalitongxue/p/4762182.html 参考: ...

  8. (转)Qt Model/View 学习笔记 (四)——创建新的Models

    创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...

  9. php 通过 create user 和grant 命令无法创建数据库用户和授权的解决办法

    php 通过 create user 和grant 命令无法创建数据库用户和授权的解决办法 解决办法, 通过 insert 命令的方式进行创建. 创建数据库用户: $sql= "insert ...

随机推荐

  1. C#中WebClient使用DownloadString中文乱码的解决办法

    原文:C#中WebClient中文乱码的解决办法 第一次尝试: string question = textBox1.Text.ToString(); WebClient client= new We ...

  2. 【水一发next_permutation】poj 1146——ID Codesm

    来源:点击打开链接 求字典序下一位,没有直接输出没有.全排列函数秒水过. #include <iostream> #include <algorithm> #include & ...

  3. 外网訪问内网应用实现之无公网IP、多port、固定port、UDP等应用的实现方法

    有公网IP时,能够通过路由映射来实现外网訪问内网.然,当没有公网IP时,怎样实现外网訪问内网应用? 硬件路由方法因为无公网不可行,能够使用软件port映射的方法.如开放的NAT123全port映射. ...

  4. xpage 获取 附件

    var db:NotesDatabase=session.getCurrentDatabase(); var doc:NotesDocument=db.getDocumentByUNID('80E21 ...

  5. Eclipse快捷键 今天又学会了几个不常用的 收藏了

    1.Ctrl+e           打开所有已经打开的文件列表,当你使用Eclipse打开了N多文件的时候,需要找到一个你之前打开过                       的文件,是不是就很费 ...

  6. HDU 2087 剪花布条 KMP入门

    Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条.计算一下能从花布条中尽可能剪出几块小饰条来呢?   Input ...

  7. CF-192-diy-2

    题目链接: http://codeforces.com/contest/330 A. Cakeminator 题目意思: 给一个r*c的矩阵方格,有些位置有S,如果某一行和一列都不含标记为S的方格,则 ...

  8. ADO.NET基础笔记

    ADO.NET 程序要和数据库交互要通过ADO.NET进行,通过ADO.Net就能在程序中执行SQL了. ADO.Net中提供了对各种不同的数据库的统一操作接口. 连接字符串: 程序通过连接字符串指定 ...

  9. WRTnode 的 HTTP Web PWM 调光实验(2016-05-16)

    前言 这里是节取自 物联网的任意门——WRTnode2R 评测 中的 http web PWM 调光灯实验,所以有一些前置设置如果没有描述清楚可参考该处. 正文 步骤一:编辑 html 文件放在 /w ...

  10. 我的Python成长之路---第七天---Python基础(22)---2016年2月27日(晴)

    socket网络编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ...