2012-08-13 66 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