/****
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. BZOJ 1002 轮状病毒 (基尔霍夫矩阵)

    题解:http://vfleaking.blog.163.com/blog/static/17480763420119685112649/ #include <iostream> #inc ...

  2. Java ThreadLocal 学习

    同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式. 而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多 ...

  3. Qt 4.6: A Quick Start to Qt Designer

    Qt 4.6: A Quick Start to Qt Designer A Quick Start to Qt Designer Using Qt Designer involves four ba ...

  4. [置顶] JDK-CountDownLatch-实例、源码和模拟实现

    Conception A synchronization aid that allows one or more threads to wait until a set of operations b ...

  5. mysql 假设存在id则设数据自添加1 ,不存在则加入。java月份计算比較

    </pre><pre name="code" class="sql">INSERT INTO invite_rejectlog_num ...

  6. YUV / RGB 格式及快速转换算法

    1 前言 自然界的颜色千变万化,为了给颜色一个量化的衡量标准,就需要建立色彩空间模型来描述各种各样的颜色,由于人对色彩的感知是一个复杂的生理和心理联合作用 的过程,所以在不同的应用领域中为了更好更准确 ...

  7. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  8. js超简单日历

    用原生js写了一个超级简单的日历.当做是练习js中的Date类型. 思路: 获取某个日期,根据年份计算出每个月的天数. 利用Date中的getDay()知道该月份的第一天为星期几. 循环创建表格,显示 ...

  9. 你能相信吗?这些都是由一个DIV元素实现的动画,纯CSS3技术

    http://www.webhek.com/misc/css-loaders

  10. 修改页面中所有TextBox控件的样式--CSS

    1.HTML <div> TextBox<br /> <input type="text" name="name" value=& ...