5

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?

Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • as docs suggests use setValue. "The store must be loaded and the list expanded for this function to work, otherwise use setValue". it works for me as expected – ncank Jul 11 '12 at 22:30

5 Answers5

11

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;
}
Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • I have spent 6 hours to know how to display the correct string and found it in ```comboBox.setRawValue(displayValue);``` – Ahmed Bermawy Jul 30 '21 at 17:04
2

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

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 2
    There must be a bug in ExtJs 3.4 because selectByValue and select both throw an exception even when the dropdown is expanded. – Greg Finzer Jun 12 '12 at 19:14
0

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);
}
Jared Price
  • 5,217
  • 7
  • 44
  • 74
0

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 }
0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135