ExtJS提供了一个具有许多功能的花哨的组合框 - 提前输入,允许随机输入文本,隐藏下拉列表中所有未加入已输入文本的条目。< / p>
我不想要这些功能。我想要一个选择框,其行为几乎与人们期望普通选择框在vanilla html中完全相同。
我确实希望它绑定到数据存储,我确实想要组合框附带的所有其他extjs配置好东西。我只是不希望用户/测试人员在遇到一个选择框时会惊慌失措,这个选择框打破了他们现有的这些工作方式的心理范式。
那么我怎样才能让extjs组合框更像选择框呢?或者我是否完全使用了错误的小部件?
答案 0 :(得分:32)
在实例化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'
,请替换您的案例中的store
和Ext.data.JsonStore
参数。
答案 1 :(得分:7)
当前接受的解决方案效果很好,但是如果有人想要一个也处理键盘输入的ComboBox,就像普通的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));
}
}
}
}
答案 2 :(得分:2)
你试过typeAhead = false
吗?不太确定这是否接近你想要的。
var combo = new Ext.form.ComboBox({
typeAhead: false,
...
});
答案 3 :(得分:2)
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
}
});
答案 4 :(得分:-3)
只需使用Ext.merge
功能
来自文档:http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-merge