代码之家  ›  专栏  ›  技术社区  ›  Spike Williams

如何让extjs组合框像普通的html选择框一样工作?

  •  20
  • Spike Williams  · 技术社区  · 15 年前

    ExtJS提供了一个奇特的组合框,它有很多特性——提前键入,允许随机输入文本,隐藏下拉列表中所有与已输入文本不匹配的条目。

    5 回复  |  直到 11 年前
        1
  •  32
  •   blong    11 年前

    在实例化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

        2
  •  7
  •   clint    14 年前

    {
        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));
                }
            }
        }
    }
    
        3
  •  2
  •   aDev    15 年前

    你试过了吗 typeAhead = false ? 不太确定这是否接近你想要的。

    var combo = new Ext.form.ComboBox({
        typeAhead: false,
    
        ...
    
    });
    
        4
  •  2
  •   Kristian    14 年前
    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
        }
     });
    
        5
  •  -3
  •   vampolo    11 年前