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); //删除客户端数据
},

%s 个评论

要回复文章请先登录注册