2012-08-13 98 views
6

我有代码:ExtJS的 - 表单提交

win = desktop.createWindow({ 
    id: 'admin-win', 
    title: 'Add administration users', 
    width: 740, 
    height: 480, 
    iconCls: 'icon-grid', 
    animCollapse: false, 
    constrainHeader: true, 
    xtype: 'form', 
    bodyPadding: 15, 
    url: 'save-form.php', 
    items: [{ 
     xtype: 'textfield', 
     fieldLabel: 'Field', 
     name: 'theField' 
    }], 

    buttons: [{ 
     text: 'Submit', 
     handler: function() { 
      var form = this.up('form').getForm(); 
      if (form.isValid()) { 
       form.submit({ 
        success: function (form, action) { 
         Ext.Msg.alert('Success', action.result.message); 
        }, 
        failure: function (form, action) { 
         Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response'); 
        } 
       }); 
      } 
     } 
    }] 
}); 

和按钮不起作用。它会产生错误 - this.up('form')是未定义的。我怎样才能在这样的代码中调用getForm()?

更新: 非常感谢您的快速回复! 我修改你的代码为我的需求,这是它,并将它与桌面实例的工作原理:

win = desktop.createWindow({ 
    id: 'admin-win', 
    title: 'Add administration users', 
    width: 740, 
    iconCls: 'icon-grid', 
    animCollapse: false, 
    constrainHeader: true, 
    items: [{ 
     xtype: 'form', 
     bodyPadding: 15, 
     url: 'save-form.php', 
     items: [{ 
      xtype: 'textfield', 
      fieldLabel: 'Field', 
      name: 'theField' 
     }], 

     buttons: [{ 
      text: 'Submit', 
      handler: function() { 
       var form = this.up('form').getForm(); 
       if (form.isValid()) { 
        this.up().up().submit({ 
         success: function (form, action) { 
          Ext.Msg.alert('Success', action.result.message); 
         }, 
         failure: function (form, action) { 
          Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response'); 
         } 
        }); 
       } 
      } 
     }] 
    }] 
}); 
+0

这是您的完整的代码?什么desktop.createWindow做什么?我问这是因为你似乎试图创建一个窗口,但使用Ext.form.Panel选项。 – davidbuzatto 2012-08-13 21:43:17

+0

它基于ExtJS桌面示例。我只想用窗体创建窗口。 – 2012-08-13 21:49:08

+0

那么你复制/粘贴这段代码? – davidbuzatto 2012-08-13 21:49:47

回答

5

正如我已经说过,看来你有你的代码的问题。您将Ext.form.Panel选项传递给Ext.window.Window(我假设这是因为您调用的方法的名称)。我正在给你写一个窗口的例子。一会儿。

它已经准备就绪。请看:

Ext.create('Ext.window.Window', { 
    title: 'This is a Window with a Form', 
    height: 200, 
    width: 400, 
    layout: 'fit', 
    items: [{ // the form is an item of the window 
     id: 'admin-win', 
     title: 'Add administration users', 
     width: 740, 
     height: 480, 
     iconCls: 'icon-grid', 
     animCollapse: false, 
     constrainHeader: true, 
     xtype: 'form', 
     bodyPadding: 15, 
     url: 'save-form.php', 
     items: [{ 
      xtype: 'textfield', 
      fieldLabel: 'Field', 
      name: 'theField', 
      allowBlank: false 
     }], 
     buttons: [{ 
      text: 'Submit', 
      handler: function() { 
       var form = this.up('form').getForm(); 
       if (form.isValid()) { 
        form.submit({ 
         success: function(form, action) { 
          Ext.Msg.alert('Success', action.result.message); 
         }, 
         failure: function(form, action) { 
          Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response'); 
         } 
        }); 
       } else { 
        Ext.Msg.alert("Error!", "Your form is invalid!"); 
       } 
      } 
     }] 
    }] 
}).show(); 

的jsfiddle:http://jsfiddle.net/davidbuzatto/vWmmD/

+0

谢谢:-)我更新了我的第一篇文章,因为我没有权限回答我的主题;-)我必须修改您的代码以满足我的需求。 – 2012-08-13 22:07:55

+0

欢迎您!正如我所说的,你确实将Ext.form.Panel选项传递给Ext.window.Window :) ExtJS非常好。看看它的文档,因为它非常完整。 – davidbuzatto 2012-08-13 22:11:34