/****
CJuiDialog for create new model http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/translated by php攻城师http://blog.csdn.net/phpgcsIntroduction
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 dialogarray('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. 参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型?

    参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型? 统计学习模型又可以分为参数化模型(para ...

  3. 成功解决raise ValueError(‘No model found in config file.‘) ValueError: No model found in config file.

    成功解决raise ValueError('No model found in config file.') ValueError: No model found in config file. 目录 ...

  4. AutoML大提速,谷歌开源自动化寻找最优ML模型新平台Model Search

    作者|魔王.杜伟 来源|机器之心 为了帮助研究者自动.高效地开发最佳机器学习模型,谷歌开源了一个不针对特定领域的 AutoML 平台.该平台基于 TensorFlow 构建,非常灵活,既可以找出最适合 ...

  5. Android 创建新Project时报错 Cannot create linked resource

    在创建新的Project的时候报错Cannot create linked resource '/.org.eclipse.jdt.core.external/folders/.link0'. The ...

  6. SQL—— Create table as select 与 使用select查询结果创建新表

    Create table as select,有以下三种方式: create table table1 as select * from table2 where 1=2; 创建一个表结构与table ...

  7. php model get set方法,创建数据库对象之__set方法

    __set( )方法自动创建数据对象原始数据其实就是通过魔术方法动态创建属性的方法来生成数据对象 2. 源码:本教程如无特别说明:源码均指:Model.php 类文件中的内容. 文件位置:/think ...

  8. oracle手动 建库_Oracle Create the Database for 11g(手动创建数据库)

    Oracle Create the Database for 11g(手动创建数据库,附整个过程自动运行脚本) 这里说的创建数据库并非是dbca调用OUI图形来进行的,而是手动建库方式. 介绍一下操作 ...

  9. 【数据分析师自学系列-MySQL】创建新表create table、create table as、create table like的区别

    [数据分析师自学系列-MySQL]创建新表create table.create table as.create table like的区别 1.create table:基本创建新表方式 格式如下: ...

最新文章

  1. 用 Winetricks 配置 WINE
  2. Git 之五 通信协议(HTTPS、SSH、Git)、使用远程仓库(GitHub、GitLab、Gitee等)
  3. TensorFlow 2.0不好用?会了是“真香”!
  4. OpenCV图像形态 image morphology的实例(附完整代码)
  5. SBT, activator, play之間的糾葛
  6. Java虚拟机(十二)——StringTable
  7. 42 github 开源代码 ——README.md语法/相关操作等
  8. PHP字符串处理函数
  9. completionService
  10. 网络流 增广路 入门很好的文章
  11. 通过ffmpeg将FLV文件转换为MP4
  12. [办公自动化]名师推荐-excelpro刘万祥 图表之道作者
  13. 手撕IP核系列——Xilinx FIFO IP核-异步FIFO
  14. 【AR】AR 的几种底层实现方式
  15. spring-boot mybadis多数据源配置
  16. 搭建L2TP-***
  17. RT5350订制WIFI转串口
  18. jzxx1177买蛋糕II
  19. win10 更新后蓝牙无法连接以前设备
  20. Elegy written in a country church-yard

热门文章

  1. 优集品 php,从细节处着眼 优集品打造成人世界的儿童节
  2. android手机活跃度,微信Android机型活跃度曝光,这个结果你满意吗?
  3. C语言讲义——字符串
  4. Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索
  5. Rsync:一个很实用的文件同步命令
  6. 【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】
  7. Spring Thread Pool 线程池的应用
  8. 企业级业务系统开发实战-序言
  9. xml学习4-dtd
  10. 一步一步SharePoint 2007之五:向网站中添加一个子网站