2013-07-16 22 views
8

我有一個組件如下:ExtJS的 - 如何獲得組件項目價值

{ 
    xtype: 'fieldcontainer', 
    layout: 'hbox', 
    id: 'article-level-container', 
    defaultType: 'textfield', 
    fieldDefaults: { 
     labelAlign: 'top' 
    }, 
    items: [{ 
     fieldLabel: 'LEVEL', 
     name: 'artLevel', 
     inputWidth: 216, 
     margins: '0 5 5 0', 
     allowBlank: false, 
     fieldStyle: 'text-align: right; font-size: 13pt; background-color: #EAFFCC;' 
    }, { 
     fieldLabel: 'VALUE', 
     name: 'artValue', 
     inputWidth: 216, 
     allowBlank: false, 
     blankText: 'zorunlu alan, boş bırakılamaz', 
     fieldStyle: 'text-align: right; font-size: 13pt; background-color: #EAFFCC;', 
     listeners: { 
      change: function(textfield, newValue, oldValue) { 
       if (oldValue == 'undefined' || newValue == '') { 
        Ext.getCmp('btnArticleSave').disable(); 
       } else { 
        Ext.getCmp('btnArticleSave').enable(); 
       } 
      } 
     } 
    }] 
} 

我想第二個項目fieldLabel值(在這種情況下的值)。

  • 如何在onReady函數之外獲得此字段值?
  • 我怎樣才能改變這個字段標籤與新值(我想選擇的組合框的值更改FieldLabel聯合)

UPDATE 我試過如下:

var artField = Ext.ComponentQuery.query('#articleValueField'); 
console.log(artField); 

Console Output

回答

16

幾種常用的方法是使用Ext.ComponentQuery

爲您的領域配置一個itemIditemId: 'theField'

var field= Ext.ComponentQuery.query('#theField')[0]; 
field.setFieldLabel(valueFromCombo); 

change監聽器添加到您的組合,你可以使用上下(這也是分量查詢)

listeners: { 
    change: function(combo) { 
    var form = combo.up('#form'); 
    var field = form.down('#theField'); 
    field.setFieldLabel(lookupValueFromCombo); 
    } 
} 

記得在Ext JS中的任何配置設置會得到一個setter和因此fieldLabelgetFieldLabel() & setFieldLabel(s)方法。

編輯上面 僅與Ext JS的4.1+ 與Ext JS的4.0+,你可以這樣做:

field.labelEl.update('New Label'); 
+2

沒有外組合框中選擇的項目更多地說。 +1 – sra

+0

我覺得我做錯了什麼。當試圖通過'ComponentQuery'獲得字段時,我可以看到控制檯中字段的值。但'getFieldLabel'和'setFieldLabel'都不起作用。我得到'setFieldLabel'不是一個函數錯誤!你有什麼主意嗎? –

+0

親愛的@sra你有什麼想法嗎? –

1

獲得組合聽者

yourComboboxName.on('change', function (combo, record, index) { 

      alert(record); // to get the selected item 

      console.log(record); // to get the selected item 

     });