我如何获得一个ExtJS组合框行为像一个正常的,HTML选择框?

StackOverflow https://stackoverflow.com/questions/2102671

  •  21-09-2019
  •  | 
  •  

ExtJS的提供了一个奇特的组合框有大量的功能 - 型进取,允许任意文本输入,躲在不与已输入的文本星下拉列表中的所有条目<。 / p>

我不希望这些功能。我希望有一个选择框的行为几乎完全一样人会期望一个正常的选择框会在香草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'store说法,你的情况,如果你想它绑定到例如Ext.data.JsonStore

其他提示

目前公认的解决方案的伟大工程,但如果有人想一个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));
            }
        }
    }
}

你有没有尝试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
    }
 });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top