如何绑定数组到arraystore,以便在extjs中填充组合

How to bind array to arrystore in order to populate combo in extjs

本文关键字:extjs 组合 填充 arraystore 何绑定 绑定 数组      更新时间:2023-09-26

我将数组设置为

var cars = new Array('audi','benz','citron','nissan','alto');

我想把这些数据添加到arraystore中,就像下面的

 var myStore = new Ext.data.ArrayStore({
        data   : cars ,
        fields : ['names']
    });

将数组存储绑定到combo

 var myCombo = new Ext.form.ComboBox({
        store: myStore ,
        displayField: 'name',
        valueField: 'name',
        typeAhead: true,
        mode: 'local',
        forceSelection: true,
        triggerAction: 'all',
        emptyText: 'Select a state...',
        selectOnFocus: true,
    });

组合只显示数组中每个单词的第一个字母a, b, c, n, a

我如何才能正确地显示组合,因为我正在使用的数组是编程填充的,然后绑定到arraystore

或者,如果您只是传递数组作为存储配置,这也将工作(假设是最新版本):

new Ext.form.ComboBox({
    store: ['Audi', 'Benz', 'Citron', 'Nissan', 'Alto']
});

注意,如果是这种情况,则不需要指定displayField/valueField

ArrayStore使用的数据格式是数组的数组。按照以下方式重新格式化您的存储数据应该允许它工作:

var cars = [['audi'], ['benz'], ['citron'], ['nissan'], ['alto']];

将您的格式转换为所需的格式非常简单:

for ( var i = 0, c = cars.length; i < c; i++ ) {
    cars[i] = [cars[i]];
}