目前已经被广泛接受的解决方案非常好,但是如果有人想要一个ComboBox也像普通的HTML select box一样处理键盘输入(例如,每次按“P”都会选择以“P”开头的下一个项目),则以下内容可能会有所帮助:
{
xtype: 'combo',
fieldLabel: 'Price',
name: 'price',
hiddenName: 'my_dropdown',
autoSelect: false,
allowBlank: false,
editable: false,
triggerAction: 'all',
typeAhead: true,
width:120,
listWidth: 120,
enableKeyEvents: true,
mode: 'local',
store: [
['val1', 'Appaloosa'],
['val2', 'Arabian'],
['val3', 'Clydesdale'],
['val4', 'Paint'],
['val5', 'Palamino'],
['val6', 'Quarterhorse'],
],
listeners: {
keypress: function(comboBoxObj, keyEventObj) {
var valueFieldName = "field1";
var displayFieldName = "field2";
var selectedIndices = this.view.getSelectedIndexes();
var currentSelectedIndex = (selectedIndices.length > 0) ? selectedIndices[0] : null;
var typedChar = String.fromCharCode(keyEventObj.getCharCode());
var startIndex = (currentSelectedIndex == null) ? 0 : ++currentSelectedIndex;
var matchIndex = this.store.find(displayFieldName, typedChar, startIndex, false);
if( matchIndex >= 0 ) {
this.select(matchIndex);
} else if (matchIndex == -1 && startIndex > 0) {
matchIndex = this.store.find(displayFieldName, typedChar, 0, false);
if( matchIndex >= 0 ) {
this.select(matchIndex);
}
}
if( matchIndex >= 0 ) {
var record = this.store.getAt(matchIndex);
this.setValue(record.get(valueFieldName));
}
}
}
}
triggerAction
为'all'; 删除了typeAhead
将mode
重命名为queryMode
- kirilloid