Extjs为存储添加了独特的组合框值

时间:2015-10-26 11:09:08

标签: extjs combobox store

我有一个连接到商店的组合框。当我在组合框中键入内容并按Enter键时,我将组合框值添加到商店。问题是我只想添加不存储的值。这是示例代码:

var states = Ext.create('Ext.data.Store', {
    fields: ['name'],
    data : [
        {name:"Alabama"},
        {name:"Alaska"},
        {name:"Arizona"}        
    ]
});

Ext.create('Ext.form.ComboBox', {
    fieldLabel: 'Choose State',
    store: states,
    queryMode: 'local',
    displayField: 'name',
    valueField: 'name',
    renderTo: Ext.getBody(),
    listeners: {
                specialkey: function(field, e){                    
                    var store = this.getStore();
                    if (e.getKey() == e.ENTER) {                       
                        store.add({name: this.getValue()});
                    }
                }
            }
});

1 个答案:

答案 0 :(得分:1)

您可以在添加之前在商店中查找该值:

specialkey: function(field, e){                    
    var store = this.getStore();

    if (e.getKey() !== e.ENTER) {  
        return;
    }  

    if(store.find('name', this.getValue(), null, null, true) === -1) {
        store.add({name: this.getValue()});
    }         
 }

带答案的fiddle