ComboBoxes can use any type of Ext.data.Store as a data source.
This means your data can be XML, JSON, arrays or any other supported format. It can be loaded using
Ajax, JSONP or locally.
The js is not minified so it is readable. See combos.js.
Remote query mode
This ComboBox uses queryMode: 'remote' to perform the query on a remote API which
returns book titles which match the typed string.
var bookStore = new Ext.data.Store({
proxy: {
type: 'jsonp',
startParam: 'startIndex',
limitParam: 'maxResults',
url: 'https://www.googleapis.com/books/v1/volumes',
reader: {
type: 'json',
totalProperty: 'totalItems',
root: 'items'
}
},
fields: [{
name: 'title',
mapping: function(raw) {
var result = raw.volumeInfo.title;
if (raw.volumeInfo.subtitle) {
result = result + ' - ' + raw.volumeInfo.subtitle;
}
return result;
}
}, {
name: 'ISBN',
mapping: function(raw) {
var ids = raw.volumeInfo.industryIdentifiers;
return ids ? ids[0].identifier : 'No identifier for this book';
}
}]
});
var bookCombo = Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Select Book',
renderTo: 'remoteQueryCombo',
displayField: 'title',
valueField: 'ISBN',
width: 500,
labelWidth: 130,
store: bookStore,
queryParam: 'q',
queryMode: 'remote',
// Do not use the default "all" for unbounded remote datasets.
// We could also have used the hideTrigger config
triggerAction: 'query',
listConfig: {
getInnerTpl: function() {
return '{%var value = this.field.getRawValue().replace(/([.?*+^$[\\]\\\\(){}|-])/g, "\\\\$1");%}' +
'{[values.title.replace(new RegExp(value, "i"), function(m) {return "<b>" + m + "</b>";})]}';
}
},
listeners: {
select: function() {
Ext.Msg.alert('Chosen book', 'Buying ISBN: ' + this.getValue());
}
}
});
Remote loaded, local query mode
This ComboBox uses remotely loaded data, to perform querying client side.
This is suitable when the dataset is not too big or dynamic to be manipulated locally
This example uses a custom template for the dropdown list to illustrate grouping.
var Country = Ext.define('Country', {
extend: 'Ext.data.Model',
fields: ['name', {
name: 'region',
mapping: 'region.value'
}]
}),
countryStore = new Ext.data.Store({
model: Country,
proxy: {
type: 'jsonp',
callbackKey: 'prefix',
limitParam: 'per_page',
url: 'http://api.worldbank.org/countries?format=jsonp',
reader: {
type: 'json',
// Response is an array where element [1] is the array of records
getData: function(raw) {
return raw[1];
}
}
},
sorters: {
property: 'region'
},
// Data includes aggregates which are not countries
filters: {
fn: function(rec) {
return rec.get('region') !== 'Aggregates'
}
},
// Load whole dataset.
// API only returns 25 by default.
pageSize: 1000,
autoLoad: true
});
var countryCombo = Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Select Country',
renderTo: 'remoteLoadedCombo',
displayField: 'name',
width: 500,
labelWidth: 130,
store: countryStore,
queryMode: 'local',
tpl: '<ul class="x-list-plain">' +
'{% var lastRegion, region; %}' +
'<tpl for=".">' +
// Only show region headers when there are more than 10 choices
'if (this.store.getCount() > 10 && region !== lastRegion) {' +
'if (region !== lastRegion) {' +
'lastRegion = region;%}' +
'<li class="x-grid-group-hd x-grid-group-title">{region}</li>' +
'{%}%}'+
'<li class="x-boundlist-item">' +
'{name}' +
'</li>'+
'</tpl>'+
'</ul>'
});
Locally loaded data
This ComboBox uses local data from a JS array:
// Define the model for a State
Ext.regModel('State', {
fields: [
{type: 'string', name: 'abbr'},
{type: 'string', name: 'name'},
{type: 'string', name: 'slogan'}
]
});
// The data store holding the states
var store = Ext.create('Ext.data.Store', {
model: 'State',
data: states
});
// Simple ComboBox using the data store
var simpleCombo = Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Select a single state',
renderTo: 'simpleCombo',
displayField: 'name',
width: 500,
labelWidth: 130,
store: store,
queryMode: 'local',
typeAhead: true
});
Custom Item Templates
This ComboBox uses the same data, but also illustrates how to use an optional
custom template to create custom UI renditions for list items by overriding the getInnerTpl method. In this case
each item shows the state's abbreviation, and has a QuickTip which displays the state's nickname when hovered over.
// Define the model for a State
Ext.regModel('State', {
fields: [
{type: 'string', name: 'abbr'},
{type: 'string', name: 'name'},
{type: 'string', name: 'slogan'}
]
});
// The data store holding the states
var store = Ext.create('Ext.data.Store', {
model: 'State',
data: states
});
// ComboBox with a custom item template
var customTplCombo = Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Select a single state',
renderTo: 'customTplCombo',
displayField: 'name',
width: 500,
labelWidth: 130,
store: store,
queryMode: 'local',
listConfig: {
getInnerTpl: function() {
return '<div data-qtip="{name}. {slogan}">{name} ({abbr})</div>';
}
}
});
Multiple Selection
This ComboBox uses the same data once again, but allows selecting multiple values.
// Define the model for a State
Ext.regModel('State', {
fields: [
{type: 'string', name: 'abbr'},
{type: 'string', name: 'name'},
{type: 'string', name: 'slogan'}
]
});
// The data store holding the states
var store = Ext.create('Ext.data.Store', {
model: 'State',
data: states
});
// ComboBox with multiple selection enabled
var multiCombo = Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Select multiple states',
renderTo: 'multiSelectCombo',
multiSelect: true,
displayField: 'name',
width: 500,
labelWidth: 130,
store: store,
queryMode: 'local'
});
Transformation
ComboBoxes can also be created from existing HTML <select> elements on the page by
specifying the transform config. This allows creation of rich ComboBox fields with autocompletion
and list filtering, in an unobtrusive way.