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

可紊 提交于 2019-12-10 13:39:38

问题


I am having array as

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

I want to add this data to arraystore like below

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

On Binding this array store to 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,

    });

The combo is showing only first letter of each word in the array as a, b, c, n, a

How can I properly disply the combo as the arry i am using is is populated programatically and then binding to arraystore


回答1:


The format of data the ArrayStore consumes is an array of arrays. Reformatting your store data as follows should allow it to work:

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

Converting from your format to the required format is simple enough:

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



回答2:


Alternatively, if you just pass the array as the store config, that will also work (assuming a recent version):

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

Note you don't need to specify displayField/valueField if this is the case.



来源:https://stackoverflow.com/questions/5631689/how-to-bind-array-to-arrystore-in-order-to-populate-combo-in-extjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!