Combo Boxes

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 states which match the typed string.

    Ext.create('Ext.form.field.ComboBox', {
        fieldLabel: 'Select State',
        renderTo: 'remoteQueryCombo',
        displayField: 'name',
        width: 500,
        labelWidth: 130,
        store: createStore({
            proxy: {
                type: 'ajax',
                url: '../shared/states_remote/states.php',
                reader: {
                    type: 'array',
                    root: 'data'
                }
            },
            data: null
        }),
        queryParam: 'q',
        queryMode: 'remote',
        listeners: {
            select: function () {
                Ext.Msg.alert('State', 'Chosen state: ' + 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.

    Ext.create('Ext.form.field.ComboBox', {
        fieldLabel: 'Select State',
        renderTo: 'remoteLoadedCombo',
        displayField: 'name',
        width: 500,
        labelWidth: 130,
        store: createStore({
            proxy: {
                type: 'ajax',
                url: '../shared/states_remote/states.js',
                reader: {
                    type: 'array'
                }
            },
            data: null,
            autoLoad: true
        }),
        queryMode: 'local',
        tpl: '<ul class="x-list-plain">' +
                '<tpl for=".">' +
                    '<li class="x-grid-group-hd x-grid-group-title">{abbr}' +
                    '<li class="x-boundlist-item">' +
                        '{name}, {slogan}' +
                    '</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.

var transformed = Ext.create('Ext.form.field.ComboBox', {
    typeAhead: true,
    transform: 'stateSelect',
    width: 135,
    forceSelection: true
});