Greg Finzer
Greg Finzer

Reputation: 7054

How to select a combobox value in ExtJs?

I am trying to simply select an item in the dropdown list after it has been loaded into a store. This does not work:

Ext.getCmp('ddlModel').setValue(aircraftStore.getAt(0).data.ModelTypeCode);

This throws an exception:

Ext.getCmp('ddlModel').selectByValue(aircraftStore.getAt(0).data.ModelTypeCode);

Here is the exception: 'this.view' is null or not an object

Anyone know how to do this in ExtJs?

Upvotes: 5

Views: 37322

Answers (5)

Joel Muñoz Moran
Joel Muñoz Moran

Reputation: 139

I think the correct way is to configure your combobox with this property:

autoSelect: true

true to select the first result gathered by the data store (defaults to true). A false value would require a manual selection from the dropdown list to set the components value unless the value of (typeAheadDelay) were true

Upvotes: 0

KrazSoftware
KrazSoftware

Reputation: 1

in my case, I needed to get the id of the combobox, then compare in an if, and thus able to pass a second window, use this method and it worked.

var ValorSeleccionado = Ext.getCmp('cmb_tipoderol_usr').getValue(); // 'cmb_tipoderol_usr' is the id of the combobox.

then compare to the action

if (ValorSeleccionado == 1 ) { Do Action }

Upvotes: 0

Jared Price
Jared Price

Reputation: 5375

In many cases you may want to set the combobox to a certain index. In ExtJs 4.2 you can do this like so:

function setIndex(combobox, value)
{
    combobox.setValue(combobox.store.data.items[value].data.field1);
}

Upvotes: 0

Greg Finzer
Greg Finzer

Reputation: 7054

I created a function to set the value of the combo box in ExtJs:

function ComboSetter(comboBox, value) {
    var store = comboBox.store;
    var valueField = comboBox.valueField;
    var displayField = comboBox.displayField;

    var recordNumber = store.findExact(valueField, value, 0);

    if (recordNumber == -1)
        return -1;

    var displayValue = store.getAt(recordNumber).data[displayField];
    comboBox.setValue(value);
    comboBox.setRawValue(displayValue);
    comboBox.selectedIndex = recordNumber;
    return recordNumber;
}

Upvotes: 11

JohnnyHK
JohnnyHK

Reputation: 311865

Ext.getCmp('ddlModel').select(aircraftStore.getAt(0));

Upvotes: 2

Related Questions