【问题标题】:Extjs add unique combobox value to storeExtjs 添加唯一的组合框值来存储
【发布时间】:2016-01-25 11:07:53
【问题描述】:

我有连接到商店的组合框。当我在组合框中输入内容并按 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()});
                    }
                }
            }
});

【问题讨论】:

    标签: extjs combobox store


    【解决方案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 有答案

    【讨论】:

    • 第一次添加记录后没有添加
    • 谢谢我的朋友。但是你能解释一下它是如何工作的吗?
    • @MarkoNikolic 我不确定你想让我解释什么。你可以在 sencha 网站上查看和find doc
    • 是的,我找到了,我明白了。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多