2010-09-28 49 views
0

我使用ExtJS 3.0,我想擴展FormPanel或任何解決方案,以便每次顯示錶單時,第一個字段都應該獲得焦點。我想加入一個全球性監聽器(如果這樣的話)這樣也許是:ExtJS添加FormPanel全局偵聽器或解決方案

afterrender: function() { 
Ext.getCmp('formAddProgPaymentDoc').findByType('textfield')[0].focus(); //// Get the first textfield and focus it 
} 

可以這樣做?我有超過40個表單,我不想爲每個表單添加一個偵聽器,我希望自動爲每個表單添加一個偵聽器。

謝謝。

回答

1

創建一個ExtOverrides.js文件,它將修改Ext的基本功能。並添加以下代碼:

Ext.override(Ext.FormPanel, { 
    onRender: function(ct, position){ 
     this.initFields(); 
     Ext.FormPanel.superclass.onRender.call(this, ct, position); 
     this.form.initEl(this.body); 

     //Begin edit of default functionality 
     var firstFieldItem = this.getForm().items.first(); 
     if(firstFieldItem){ 
      //delay the focus for 500ms to make sure the field is visible 
      firstFieldItem.focus(true,500); 
     } 
     //End edit of default functionality 
    } 
}) 

您使用ExtOverrides時,雖然需要小心 - 當你更新到內線的新版本中,你需要確認你更新的覆蓋默認功能。

+0

感謝您的迴應,我有點做了這部分,我有聽衆,但我不知道如何以及在哪裏擴展。有任何想法嗎 ? – 2010-09-28 17:09:44

+0

請參閱更新後的答案 - 希望有所幫助。 – sdavids 2010-09-28 19:08:06

+0

請注意,在重新使用對話框窗口(使用hide/show)的情況下,窗口的FormPanel的onRender函數只會在第一次顯示對話框時調用。在這種情況下,需要基於Window'show'事件的解決方案。 – 2011-01-01 16:37:40