Các giải pháp chấp nhận hiện nay hoạt động tuyệt vời, nhưng nếu có ai muốn một ComboBox mà cũng xử lý đầu vào bàn phím giống như một hộp chọn HTML đơn giản (ví dụ, mỗi lần bạn nhấn "P" được chọn mục tiếp theo trong danh sách bắt đầu với "P"), sau đây có thể hữu ích:
{
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) {
// Ext.Store names anonymous fields (like in array above) "field1", "field2", etc.
var valueFieldName = "field1";
var displayFieldName = "field2";
// Which drop-down item is already selected (if any)?
var selectedIndices = this.view.getSelectedIndexes();
var currentSelectedIndex = (selectedIndices.length > 0) ? selectedIndices[0] : null;
// Prepare the search criteria we'll use to query the data store
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) {
// If nothing matched but we didn't start the search at the beginning of the list
// (because the user already had somethign selected), search again from beginning.
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));
}
}
}
}
Nguồn
2011-03-24 16:20:00
Cảm ơn - Tôi đã tìm thấy một số trong số đó, nhưng đó là "triggerAction:" tất cả "" là điều quan trọng mà tôi đã bỏ lỡ. Nếu không có điều đó, trình đơn thả xuống sẽ ẩn tất cả các mục khác với mục đã được chọn. –
Dường như đây là dành cho ExtJS3. Dưới đây là những thay đổi cho Ext4: 'triggerAction' là 'tất cả' theo mặc định; 'typeAhead' bị xóa ' mode' được đổi tên thành 'queryMode' – kirilloid