EXT remote-loading forms with Combo boxes

Something that's harder than it should be is populating an EXT edit form with form data, where one of the form fields is a select box. If there is a specific set of values that this select can take, then you can hard code that using a SimpleStore, like this:


var exampledata = [['AL', 'Alabama'],
['AK', 'Alaska'],
['AZ', 'Arizona'],
['AR', 'Arkansas'],
['CA', 'California']];

var store = new Ext.data.SimpleStore({
fields: ['abbr', 'state'],
data : exampleData
});

var combo = new Ext.form.ComboBox({
store: store,
displayField: 'state',
valueField: 'abbr',
... etc ...
});

form = new Ext.form({
items: [combo],
... etc ...
});

form.load(url_to_load_from);

var exampledata = [['AL', 'Alabama'],
['AK', 'Alaska'],
['AZ', 'Arizona'],
['AR', 'Arkansas'],
['CA', 'California']];

var store = new Ext.data.SimpleStore({
fields: ['abbr', 'state'],
data : exampleData
});

var combo = new Ext.form.ComboBox({
store: store,
displayField: 'state',
valueField: 'abbr',
... etc ...
});

form = new Ext.form({
items: [combo],
... etc ...
});

form.load(url_to_load_from);

So that will populate the select box with the static values you've defined (the 5 states above), then when the form loads it will select the appropriate option automatically.

So far so good, but what if you need to load what goes into the select box dynamically? Well, first you'll need to set up your remote data store (my server is sending back JSON data, hence the JsonReader):


store = new Ext.data.Store({
url: 'url_to_load_combo_data_from',
reader: new Ext.data.JsonReader(
{root: 'states',totalProperty: 'results'},
[
{name: 'name', type: 'string', mapping: 'state.name'},
{name: 'abbr', type: 'string', mapping: 'state.abbr'}
]
)
});

store = new Ext.data.Store({
url: 'url_to_load_combo_data_from',
reader: new Ext.data.JsonReader(
{root: 'states',totalProperty: 'results'},
[
{name: 'name', type: 'string', mapping: 'state.name'},
{name: 'abbr', type: 'string', mapping: 'state.abbr'}
]
)
});

This will consume data like this:


{
"states": [
{"state": {"name": "Alabama", "abbr": "AL"}},
{"state": {"name": "Alaska", "abbr": "AK"}}
],
"results": "2"
}

{
"states": [
{"state": {"name": "Alabama", "abbr": "AL"}},
{"state": {"name": "Alaska", "abbr": "AK"}}
],
"results": "2"
}

And populate the store with a collection of state records which can then be loaded into the combobox.

Then all you need to do is load the store before loading the form data, and your comboboxes will be correctly populated, displaying the correct option. Here's the full example:

store = new Ext.data.Store({
url: 'url_to_load_combo_data_from',
reader: new Ext.data.JsonReader(
{root: 'states',totalProperty: 'results'},
[
{name: 'name', type: 'string', mapping: 'state.name'},
{name: 'abbr', type: 'string', mapping: 'state.abbr'}
]
)
});

var combo = new Ext.form.ComboBox({
store: store,
displayField: 'state',
valueField: 'abbr'
... etc ...
});

form = new Ext.formPanel({
items: [combo]
});

store.load();
form.load(your_form_data_url);
store = new Ext.data.Store({
url: 'url_to_load_combo_data_from',
reader: new Ext.data.JsonReader(
{root: 'states',totalProperty: 'results'},
[
{name: 'name', type: 'string', mapping: 'state.name'},
{name: 'abbr', type: 'string', mapping: 'state.abbr'}
]
)
});

var combo = new Ext.form.ComboBox({
store: store,
displayField: 'state',
valueField: 'abbr'
... etc ...
});

form = new Ext.formPanel({
items: [combo]
});

store.load();
form.load(your_form_data_url);

Be wary using the pagination options on the combobox here (see http://extjs.com/deploy/dev/docs/?class=Ext.form.ComboBox) - the reason being if your state's 'abbr' features on the second page of the results it won't populate the correct options into the combo box.

Share Post:

What to Read Next

To enhance your understanding of data handling with Ext JS, you might find Force Ext.data.Store to use GET insightful, as it addresses common issues with request methods in Ext JS stores. Additionally, Proxies in Ext JS 4 provides an in-depth look at managing data operations, which would complement your reading about remote-loading forms and comboboxes.