javascript - 更改 Ext-JS 组合框上的 valueField 配置?

标签 javascript user-interface extjs combobox config

我的表单上有一个组合框,我需要在其中重置其存储以及“displayField”和“valueField”配置。

通过 cmb.bindStore(newStore) 重置存储效果很好。

设置cmb.displayField = 'newfieldname';也很好用。

但是,cmb.valueField = 'newValField'; 不起作用。组合显示正确的内容,但是当我选择一个项目时,该值使用旧的 valueField 值,而不是新的值。

我已经尝试过:

  • 之后执行cmb.reset()
  • Ext.apply(...)

是因为 valueField 有点特殊,因为它是必填字段吗?是否有一些特殊的方法可以在我不知道的 Ext-JS 组件上设置配置值,或者只是无法更改“valueField”的值?

仅供引用 - 这是我的代码:

    comp.bindStore(Ext.create('Ext.data.Store', {
        fields : [ {
            name : 'abbr',
            type : 'string'
        }, {
            name : 'name',
            type : 'string'
        }, {
            name : 'slogan',
            type : 'string'
        } ],
        data : [ {
            "abbr" : "AL",
            "name" : "Alabama",
            "slogan" : "The Heart of Dixie"
        }, {
            "abbr" : "AK",
            "name" : "Alaska",
            "slogan" : "The Land of the Midnight Sun"
        }, {
            "abbr" : "AZ",
            "name" : "Arizona",
            "slogan" : "The Grand Canyon State"
        }, {
            "abbr" : "AR",
            "name" : "Arkansas",
            "slogan" : "The Natural State"
        }, ]
    }));

    comp.displayField = 'abbr';    // THIS WORKS
    comp.valueField = 'abbr';      // THIS DOESNT WORK

最佳答案

您已经快到了,但是您查看了错误的属性,因为 valueField 不是您的问题,而是 displayField。您的确切问题是预配置和缓存的属性。第一个是显示模板,第二个是选择器实例。您需要覆盖模板并删除选择器实例。这是一个工作片段 ( JSFiddle )

在示例中,我添加了第二个带有十字的触发器。点击它,组合框就会获得新值。我建议您通过从 ComboBox 扩展来创建自己的组件,并将所有内容包装到需要树参数的重新配置方法中。

Ext.onReady(function() {
    // The data store containing the list of states
    var states = Ext.create('Ext.data.Store', {
        fields: ['abbr', 'name'],
        data : [
            {"abbr":"AL1", "name":"Alabama1"},
            {"abbr":"AK1", "name":"Alaska1"},
            {"abbr":"AZ1", "name":"Arizona1"}
            //...
        ]
    });

    var comp = Ext.create('Ext.form.field.ComboBox', {
        fieldLabel: 'Choose State',
        id: 'combo-ident',
        trigger2Cls: 'x-form-clear-trigger',
        onTrigger2Click: function (args) {
            var comp = Ext.getCmp('combo-ident');
            comp.clearValue();
            comp.bindStore(Ext.create('Ext.data.Store', {
                fields : [ {
                    name : 'abbr',
                    type : 'string'
                }, {
                    name : 'name',
                    type : 'string'
                }, {
                    name : 'slogan',
                    type : 'string'
                } ],
                data : [ {
                    "abbr" : "AL",
                    "name" : "Alabama",
                    "slogan" : "The Heart of Dixie"
                }, {
                    "abbr" : "AK",
                    "name" : "Alaska",
                    "slogan" : "The Land of the Midnight Sun"
                }, {
                    "abbr" : "AZ",
                    "name" : "Arizona",
                    "slogan" : "The Grand Canyon State"
                }, {
                    "abbr" : "AR",
                    "name" : "Arkansas",
                    "slogan" : "The Natural State"
                }, ]
            }));

            comp.displayField = 'abbr';
            comp.valueField = 'name';
            comp.displayTpl = new Ext.XTemplate(
                '<tpl for=".">' +
                    '{[typeof values === "string" ? values : values["' + comp.displayField + '"]]}' +
                    '<tpl if="xindex < xcount">' + comp.delimiter + '</tpl>' +
                '</tpl>'
            );
            comp.picker = null;
        },
        store: states,
        queryMode: 'local',
        displayField: 'name',
        valueField: 'abbr',
        renderTo: Ext.getBody()
    });
    comp.on('select', function(){ console.log(arguments); console.log(arguments[0].getSubmitValue()); })

});

关于javascript - 更改 Ext-JS 组合框上的 valueField 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14591063/

相关文章:

ios - Swift ios导航栏在被滚动隐藏后不会出现

c++ - 必须在 GUI 线程中创建小部件错误!。如何更正代码?

c++ - Irrlicht Gui 鼠标不会点击按钮

javascript - 在 <div> 元素上如何确定 href 和 onClick 调用的优先级?

javascript - 给定名称作为字符串,如何检查 JavaScript 对象?

javascript - 每隔几毫秒在给定的 x-y 坐标上显示元素

javascript - Node.js 中的 Python 多处理

javascript - 使用 gulp 构建 extjs

ajax - Extjs 自定义阅读器

javascript - 不要在 $location.hash 之后添加 hash 到 URL