ExtJS 提供了一个花哨的组合框,它有很多功能——提前输入,允许随机输入文本,隐藏下拉列表中所有不以已经输入的文本加星标的条目。
我不想要这些功能。我想要一个选择框,它的行为与普通 html 中的普通选择框非常相似。
我确实希望它绑定到数据存储,并且我确实希望组合框附带的所有其他 extjs 配置好东西。我只是不希望用户/测试人员在遇到一个破坏了他们现有的关于这些事情如何工作的心理范式的选择框时吓坏了。
那么如何让 extjs 组合框更像一个选择框呢?还是我完全使用了错误的小部件?
当您实例化 Ext.form.ComboBox 对象时,您只需使用正确的配置即可获得该行为:
var selectStyleComboboxConfig = {
fieldLabel: 'My Dropdown',
name: 'type',
allowBlank: false,
editable: false,
// This is the option required for "select"-style behaviour
triggerAction: 'all',
typeAhead: false,
mode: 'local',
width: 120,
listWidth: 120,
hiddenName: 'my_dropdown',
store: [
['val1', 'First Value'],
['val2', 'Second Value']
],
readOnly: true
};
var comboBox = new Ext.form.ComboBox(selectStyleComboboxConfig);
如果您希望将其绑定到例如,请替换您的案例中的mode: 'local'
and参数。store
Ext.data.JsonStore
当前接受的解决方案效果很好,但是如果有人想要一个像普通 HTML 选择框一样处理键盘输入的组合框(例如,每次按“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) {
// 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));
}
}
}
}
你试过了typeAhead = false
吗?不太确定这是否接近您想要的。
var combo = new Ext.form.ComboBox({
typeAhead: false,
...
});
var buf = [];
buf.push('<option>aA1</option>');
buf.push('<option>aA2</option>');
buf.push('<option>bA3</option>');
buf.push('<option>cA4</option>');
var items = buf.join('');
new Ext.Component({
renderTo: Ext.getBody(),
autoEl: {
tag:'select',
cls:'x-font-select',
html: items
}
});
只需使用Ext.merge
功能
来自文档:http ://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-merge