Javascript 来自本地数组的 ExtJs 组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4884406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:35:53  来源:igfitidea点击:

ExtJs Combobox from local array

javascripthtmlcomboboxextjsarraylist

提问by Precise-One

I am trying to populate a Ext Js combo box using local array list. In the Ext Js examples, the combo is populated from a different states.js file.

我正在尝试使用本地数组列表填充 Ext Js 组合框。在 Ext Js 示例中,组合是从不同的 states.js 文件填充的。

In my example the data should come from local variable. Its not working.

在我的示例中,数据应该来自局部变量。它不工作。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Combo Boxes</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
<script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../ext-all.js">
</head>

<body>
<script type="text/javascript">
var  exampleData2 = [['1', 'hello'],['2', 'hi'],['3', 'bye']];
Ext.onReady(function(){
Ext.QuickTips.init();

// simple array store
var store = new Ext.data.ArrayStore({
    fields: ['abbr', 'state'],
    data : exampleData2 
});
  var combo = new Ext.form.ComboBox({
    store: store,
    displayField:'state',
    typeAhead: true,
    mode: 'local',
    forceSelection: true,
    triggerAction: 'all',
    emptyText:'Select a state...',
    selectOnFocus:true,
    applyTo: 'local-states'
    });
  </script>

<div>
<input type="text" id="local-states" size="20"/>
</div>
<div id="local-states" style="margin-top:10px">

</body>
</html>

回答by bensiu

scope, scope, scope

范围,范围,范围

Ext.onReady(function(){ 
  Ext.QuickTips.init();  // simple array store 
  var exampleData2 = [['1', 'hello'],['2', 'hi'],['3', 'bye']];
  var store = new Ext.data.ArrayStore({
     fields: ['abbr', 'state'],
     data : exampleData2 
     // or even better data : [['1', 'hello'],['2', 'hi'],['3', 'bye']]
     // next to change: combo.getStore().loadData( new_table );
  });
  var combo = new Ext.form.ComboBox({
     store: store,
     displayField:'state',
     typeAhead: true,
     mode: 'local',
     forceSelection: true,
     triggerAction: 'all',
     emptyText:'Select a state...',
     selectOnFocus:true,
     applyTo: 'local-states'
   });
});

to get more complex solution

获得更复杂的解决方案

Ext.ux.states = Ext.Extend ( Ex.form.ComboBox, { ....

回答by Mr. Polywhirl

Below, I have created a store, called Ext.data.FlatStore, which extends the default ArrayStore. During construction, the configured data is processed.

下面,我创建了一个名为 的商店Ext.data.FlatStore,它扩展了默认的ArrayStore. 在构建期间,将处理配置的数据。

{
    xtype: 'combo',
    queryMode: 'local',
    store: Ext.create('Ext.data.FlatStore', {
        data: [ 'yes', 'no', 'maybe' ]
    })
}

Demo @ JSFiddle

演示@JSFiddle

Ext.require(['*']);

String.capitalize = function (str) {
    return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
};

Ext.define('Ext.data.FlatStore', {
    extend: 'Ext.data.ArrayStore',
    config: {
        data: []
    },
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name : 'value'
    }, {
        name: 'display',
        type: 'string',
        convert: function (newValue, model) {
            return String.capitalize(model.get('value'));
        }
    }],
    constructor: function (config) {
        var me = this;
        config.data = config.data.map(function (value, index, values) {
            return [index, value];
        });
        me.callParent(arguments);
        me.loaded = true;
    }
}),

Ext.define('App.view.MainView', {
    extend: 'Ext.panel.Panel',
    xtype: 'mainView',
    alias: 'widget.mainview',
    controller: 'mainView',
    title: 'Outer Panel',
    referenceHolder: true,
    layout: {
        type: 'border'
    },
    initComponent: function () {
        var me = this;
        me.items = [{
            region: 'center',
            xtype: 'panel',
            title: 'Inner Panel',
            margin: 20,
            bodyStyle: 'padding: 8px;',
            layout: 'vbox',
            items: [{
                xtype: 'combo',
                store: Ext.create('Ext.data.FlatStore', {
                    data: [ 'yes', 'no', 'maybe' ]
                }),
                displayField: 'display',
                valueField: 'value',
                fieldLabel: 'Response',
                typeAhead: true,
                queryMode: 'local',
                forceSelection: true,
                triggerAction: 'all',
                emptyText: 'Choose...',
                selectOnFocus: true,
                applyTo: 'local-states'
            }]
        }],
        me.callParent();
    }
});

Ext.define('App.controller.MainViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.mainView',
    init: function () {
        var me = this;
    }
});

Ext.define('App.app.App', {
    extend: 'Ext.app.Application',
    name: 'App',
    launch: function () {
        Ext.create('Ext.Viewport', {
            layout: 'fit',
            flex: 1,
            items: [{
                xtype: 'mainview'
            }]
        });
    }
});

Ext.onReady(function () {
    Ext.application('App.app.App');
});