如何在extjs 4.2中包装表单的提交数据?

时间:2013-04-29 07:26:51

标签: forms extjs submit

我现在提交:

method: save
name: Michael
birthday: 1983-02-01

但我需要:

method: save
data[name]: Michael
data[birthday]: 1983-02-01

和字段名称必须与birthday而非data[birthday]相同。

1 个答案:

答案 0 :(得分:5)

假设您正在处理表单提交,其中您有一个代表表单的控件:

var formData = form.getFieldValues();

From Ext.form.Basic.getFieldValues

然后通过ajax提交:

Ext.Ajax.request({url: "postlocation.php", method: "POST", data: formData});

From Ext.Ajax.request

如果您不想提交表单,可以覆盖表单上的按钮以调用模拟提交的流程。

// form def up here
buttons: [
    text: "Pseudo-Submit",
    id: "altsubmitbuttonthing"
]

在你的控制器中(或按钮的事件处理程序:

this.control({
     "button[id=altsubmitbuttonthing]": {
         click: function (control) {
             var form = control.up("form"), // <- now you have your form and you can do whatever you want with it's data.
                 formData = form.getFieldValues(),
                 preparedData = {};

             formData.theDateField = new Date(data.theDateField);
             formData.theIntField = parseInt(data.theIntField, 10);

             preparedData.data.birthday = formData.birthday;
             preparedData.data.name = formData.name;

             Ext.Ajax.request({
                 url: "/submissions",
                 method: "POST",
                 type: "json",
                 data: preparedData
             });
         }
     }
});