arrays - 如何使用组合框的第一个存储值作为默认值EXTJS

标签 arrays extjs

我需要使用组合框中出现的第一个值
像这样直接使用值

xtype: 'combobox',
fieldLabel: 'Type',
name: 'type',
store: [
    'Bike'
    'Car',
    'Truck'
],
value: 'Bike' // this value

这很容易,因为给出了第一个值,但我需要像下面这样动态的

xtype: 'combobox',
fieldLabel: 'Type',
name: 'type',
store: this.type,
// value

它从数据库中获取类型,因此我不知道组合框中的第一个值

最佳答案

您将需要使用商店的加载事件来对数据加载执行操作。之后你就可以使用ComboBox的

select method

设置值。

这是工作代码:

Ext.application({
    name: 'Fiddle',

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

        Ext.create({
            xtype: 'panel',
            title: 'Parent Panel',
            frame: true,
            renderTo: Ext.getBody(),
            height: 500,
            width: 400,

            layout: {
                type: 'vbox',
                align: 'stretch'
            },

            items: [{
                xtype: 'combobox',
                store: states,
                displayField: 'name',
                valueField: 'abbr',
                listeners: {
                    afterrender: function(combo) {
                        combo.store.on('load', function(store, records) {
                            combo.select(records[0]);
                            console.log(combo, records);
                        });
                        combo.store.load();
                    }
                }
            }],

            collapsible: true
        });
    }
});

这是工作 fiddle 链接: https://fiddle.sencha.com/#view/editor&fiddle/2kgg

关于arrays - 如何使用组合框的第一个存储值作为默认值EXTJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51857549/

相关文章:

php - 重温 PHP 中如何从多维数组中删除重复值

c - 在C中输出两个二进制字符串及其十进制值相加的结果

javascript - 从数组中删除重复值

javascript - 从 ExtJS 4 中的表单卸载/取消绑定(bind)记录的正确方法是什么?

extjs - 在现有应用程序中使用 Sencha Cmd

extjs - 如何在 EXTJS Grid 组件栏中添加多个编辑器配置?

c# - 如何使用 Asp.net Websrvice 绑定(bind) ExtJS 网格?

c++ 以 'enter' 命中结束循环

unit-testing - ExtJS 检查元素是否可见

java - 使用插入排序算法,使用对象数组中的多个字段