Populate ExtJS 3.4 combobox with local JSON data

Calypo Gunn picture Calypo Gunn · Dec 24, 2012 · Viewed 13k times · Source

I'm using ExtJS 3.4 and I need to populate a combobox with the following data:

"[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]"

Which comes form a previous XMLHttpRequest, and I've got it stored in a variable so:

my_variable = "[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]"

So, I have the following ExtJS 3.4 combobox:

cbxSelDomini = new Ext.form.ComboBox({
                            hiddenName: 'Domini',
                            name: 'nom_domini',
                            displayField: 'nom_domini',
                            valueField: 'cod_domini',
                            mode: 'local',
                            triggerAction: 'all',
                            listClass: 'comboalign',
                            typeAhead: true,
                            forceSelection: true,
                            selectOnFocus: true,
                            store: mystore
});

This combobox is suposed to get data from an Ext.data.Store I named "mystore":

store: mystore = new Ext.data.Store({
                                    autoload: true,
                                    reader: new Ext.data.ArrayReader(
                                        {
                                            idIndex: 0  // id for each record will be the first element
                                        }),
                                    data: dataprova,
                                    fields: [ 
                                                        {type: 'integer', name: 'cod_domini'},
                                                        {type: 'string', name: 'nom_domini'}
                                                    ]
                                    }),

My first problem is that at first instance, data is not loaded to dataStore, even telling explicitly: mystore.loadData(my_variable);

Can somebody try to tell me what I'm doing wrong? In fireBug I get errors like "this.data is not defined", "this.reader is not defined", or "b is undefined" and "h is undefined".

The same kind of errors come when I change data format as javascript array like:

var dataexample = [[1, 'Sant Esteve de Palautordera'], [2, 'Parc Natural del Montseny']];

and call dataexample on store "data" property.

I'm absolutely lost...

Answer

Krzysztof picture Krzysztof · Dec 29, 2012

Data in variable my_variable is in JSON format, so JsonReader should be used. To use that reader you can user simply JsonStore. Example:

var data = '[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]';

var mystore = new Ext.data.JsonStore({
    //autoload: true,
    fields: [ 
        {type: 'integer', name: 'cod_domini'},
        {type: 'string', name: 'nom_domini'}
    ]
});
mystore.loadData(Ext.decode(data)); // decode data, because it is in encoded in string

var cbxSelDomini = new Ext.form.ComboBox({
    hiddenName: 'Domini',
    name: 'nom_domini',
    displayField: 'nom_domini',
    valueField: 'cod_domini',
    mode: 'local',
    triggerAction: 'all',
    listClass: 'comboalign',
    typeAhead: true,
    forceSelection: true,
    selectOnFocus: true,
    store: mystore
});

Working sample: http://jsfiddle.net/Ajnw7/