【问题标题】:ExtJS: Custom ComboBoxExtJS:自定义组合框
【发布时间】:2013-11-18 10:16:16
【问题描述】:

我只是想创建一个自定义 ComboBox 来减少一些样板:

Ext.define('App.AutoComboBox', {
    extend: 'Ext.form.field.ComboBox',
    alias: 'widget.autocombobox',

    states: null,

    initComponent: function() {
        this.callParent(arguments);
        if (!this.states) {
            this.queryMode = 'remote';
        } else {
            this.queryMode = 'local';
            this.bindStore(Ext.create('Ext.data.Store', {
                type: 'array',
                fields: ['_placeholder_'],
                data: _.map(this.states, function(state) {
                    return {_placeholder_ : state}; })
            }));
            this.displayField = this.valueField = '_placeholder_'
        }
        this.validator =  function(v) {
            var field = this.displayField,
                index = this.getStore().findExact(field, v);
            return (index!==-1) ? true : 'Invalid selection';
        };
    },
    listeners: {
        select: function(combo, records) {
            console.log(combo.getStore().indexOf(records[0])); // !== -1
        }
    }
});

这样我就可以像这样使用它:

requires: ['App.AutoComboBox'],
...
items: [{
    xtype: 'autocombobox',
    name: 'test_local',
    fieldLabel: 'test_local',
    states: [ 'cat', 'dog' ]  // local
}, {
    xtype: 'autocombobox',
    name: 'test_remote',
    fieldLabel: 'test_remote',
    store: 'Chipmunks', // a remote store
    displayField: 'chipmunk_name'
}]
...

但是有些不对劲。 AutoComboBox 呈现正常,显示下拉记录很好,但是当我 select 从下拉列表中选择一个项目时,未设置组合框的显示字段。商店似乎找到了选定的记录(如select 侦听器所见),但值仍未设置...

帮助?谢谢。

编辑:通过移动this.callParent(arguments) 新商店绑定后修复。现在接受解释为什么可以解决它的答案...(我不知道它为什么有效..但确实如此)

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:

    在父initComponent方法中,displayField用于创建displayTpl

        if (!me.displayTpl) {
            me.displayTpl = new Ext.XTemplate(
                '<tpl for=".">' +
                    '{[typeof values === "string" ? values : values["' + me.displayField + '"]]}' +
                    '<tpl if="xindex < xcount">' + me.delimiter + '</tpl>' +
                '</tpl>'
            );
        } else if (Ext.isString(me.displayTpl)) {
            me.displayTpl = new Ext.XTemplate(me.displayTpl);
        }
    

    bindStore 调用可能跟它无关,我相信这是必须放在调用父方法之前的这一行:

    this.displayField = this.valueField = '_placeholder_';
    

    【讨论】:

    • 啊,你确实是对的!感谢您抽出宝贵时间!非常感谢!
    猜你喜欢
    • 2013-06-22
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2013-01-06
    • 2011-08-19
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多