2012-08-24 124 views
9

我有一個組合框的下面的代碼,我怎麼能得到在組合框中選擇的值,並將該值加載到變量中,並在以後使用它。如何獲取extjs組合框的值?

謝謝

Ext.define('Column', { 
    extend: 'Ext.data.Model', 
    fields: ['data1', 'Data2'] 
}); 

var store = Ext.create('Ext.data.Store', { 
    model: 'Column', 
    autoLoad: true, 
    proxy: { 
     type: 'ajax', 
     url: '/data.xml', 
     reader: { 
      type: 'xml', 
      record: 'result' 
     } 
    } 
}); 

var simpleCombo = Ext.create('Ext.form.field.ComboBox', { 
    store: store, 
    displayField: 'data1', 
    valueField: 'data1', 
    width: 250, 
    labelWidth: 120, 
    fieldLabel: 'select a value', 
    renderTo: 'simpleCombo', 
    queryMode: 'local', 
    typeAhead: true 
}); 
+0

你使用的是什麼版本的Ext? – sitifensys

+0

@sitifensys基於Ext.define&Models的使用,它必須是某個版本的4.x – sra

回答

10

只需使用select事件

var simpleCombo = Ext.create('Ext.form.field.ComboBox', { 
      store: store, 
      displayField: 'data1', 
      valueField: 'data1' , 
      width: 250, 
      labelWidth: 120, 
      fieldLabel: 'select a value', 
      renderTo: 'simpleCombo', 
      queryMode: 'local', 
      typeAhead: true, 
      listeners: { 
       select: function(combo, records) { 
        // note that records are a array of records to be prepared for multiselection 
        // therefore use records[0] to access the selected record 
       } 
     }); 

API Link

其他內容從評論:

查看組合框的multiSelect屬性。您可以獲得由定義的分隔符分隔的所有值,並且select事件將爲您提供一個包含多個記錄的記錄數組。注意,getValue()只給你定義的displayField,它是一個字符串,而不是記錄本身。所以使用iComboValue [0]會給你第一個字符。應始終使用所選事件訪問所選記錄。但是您可以將它們存儲在數組中供以後使用,並用任何新選擇覆蓋它。

+0

謝謝。我用記錄[0],我得到錯誤。但是當我嘗試使用var iComboValue = simpleCombo.getValue(); \t \t \t \t Ext.Msg.alert('你好,這是標題',iComboValue); 。它爲我工作。如果我使用iComboValue [0],它只會給我選擇選項的第一個字母。我怎麼能做多選擇,然後存儲這些選定的值?謝謝 – shiro

+0

@shiro看看[multiSelect](http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.ComboBox-cfg-multiSelect)屬性組合框。您可以獲得由定義的分隔符分隔的所有值,並且select事件將爲您提供一個包含多個記錄的記錄數組。注意,getValue()只給你定義的displayField,它是一個字符串,而不是記錄本身。所以使用iComboValue [0]會給你第一個字符。應始終使用所選事件訪問所選記錄。但是您可以將它們存儲在數組中供以後使用,並用任何新選擇覆蓋它。 – sra

+0

'var i = simpleCombo.getValue();'是一個更好的解決方案。 – pllee

7

您還可以使用:

var iComboValue = simpleCombo.getValue(); 
0

可能是你應該嘗試這種

// to get the combobox selected item outside the combo listener 
     simpleCombo.on('change', function (combo, record, index) { 

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

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

     }); 
0

當使用combo.getValue()(如Izhaki指出),請確保已配置組合框正確使用商店模型中的相關字段。

存儲與值字段和名稱字段

var bandsStore = Ext.create('Ext.data.Store', { 
    fields: ['color', 'name'], 
    data : [ 
    {"color": "green", "name": "Green Day"}, 
    {"color": "pink", "name": "Pink Floyd"}, 
    {"color": "purple", "name": "Deep Purple"} 
    ] 
}); 

使用色域的呆了一會兒今天工作了價值

}, { 
    xtype: 'combo', 
    id: 'BandsCombo', 
    fieldLabel: 'Band Names', 
    name: 'bandNames', 
    store: bandsStore, 
    displayField: 'name', 
    valueField: 'color' 
}, { 

組合框爲什麼我的形式保持爲ComboBox字段發佈null。這是問題。 :)