您的位置:首页 > 其它

Ext面向对象开发实践(续)

2008-11-17 22:19 471 查看
我的上一篇文章《Ext面向对象开发实践》中简述了如何编写一个面向对象的数据维护小程序,但这一些都是基于一个客户端数据,即用户一旦刷新页面,所有的操作都将丢失,现在我们就接着上一篇文章来继续讲一下如何对数据表进行增、删、改、查操作。



要实现对数据表中的数据进行操作,第一步就是要取得数据表中的数据,我们把上篇文章中的创建Store的方法也略作调整,让其从数据表中读取数据。

this.departmentStore = new Ext.data.JsonStore({

proxy: new Ext.data.HttpProxy({url: "http://localhost:8080/Test_EXT/DB/Department.php"}),

fields: ["department_code", "department_name", "manager", "division_code"]

});

Department.php,负责连接SQL数据库,取得数据并将其转换为JSON格式,为Ext的读取作准备。

<?php

require('JSON.php');

require('uai_Personal_Info.php');

$p = new uai_Personal_Info();

$result = $p->getDepartmentList();

$json = new Services_JSON();

echo $json->encode($result);

还有一点要修改的就是新增和修改窗体的onSubmitClick方法

onSubmitClick: function() {

if (this.url != "") {

this.form.submit({url: this.url, success: this.onSubmit,

waitTitle: "Save Data", waitMsg: "Transcation process.....", scope: this});

this.fireEvent("submit", this, this.form.getValues());

}

},

Submit方法需要传递一系列参数:

url:数据处理的URL地址,这里传入的是一个负责处理新增操作的URL

success:如果提交数据处理成功,则会回调这个参数指定的处理代码

waitTitle:数据提交时弹出对话框的标题

waitMsg:数据提交时弹出对话框的信息内容

scope:回调函数中的this所指对象



这里需要说明的是处理数据的PHP文件中,必须返回一个JSON字串,如果包含"success: true",则表示处理成或,否则认为处理失败。例如下面的代码





<?php

require('JSON.php');

require('uai_Personal_Info.php');

$rs = $_POST;

$rs["success"] = true; //表示处理成功

$sql = "INSERT INTO uai_department(department_code, department_name, manager, division_code) VALUES('" .

$_POST["department_code"] . "', '" . $_POST["department_name"] . "', '" . $_POST["manager"] . "', '" . $_POST["division_code"] . "')";

$p = new uai_Personal_Info();

$rs["r"] = $p->insert_department($sql);

$json = new Services_JSON();

echo $json->encode($rs);



删除的处理则与新增、修改略有不同,因为删除不需要弹出窗体对数据进行操作,所以我们改用Ext.Ajax对象

remove: function() {

var r = this.getActiveRecord();

Ext.Ajax.request({url: "http://localhost:8080/Test_EXT/DB/delete_dept.php", params: {department_code: r.get("department_code")}});

this.getStore().remove(r); //删除客户端数据

},
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: