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

发布于 2024-08-18 16:56:43 字数 279 浏览 6 评论 0原文

ExtJS 提供了一个精美的组合框,它具有很多功能 - 提前输入、允许随机文本输入、隐藏下拉列表中不以已输入文本开头的所有条目。

我不想要这些功能。我想要一个选择框,其行为与普通 html 中的普通选择框非常相似。

我确实希望它绑定到数据存储,并且我确实想要组合框附带的所有其他 extjs 配置好东西。我只是不想让用户/测试人员在遇到一个选择框时感到惊慌,因为这个选择框打破了他们现有的这些东西如何工作的思维模式。

那么如何才能让 extjs 组合框更像选择框呢?或者我完全使用了错误的小部件?

ExtJS provides a fancy combo-box that has lots of features - type ahead, allowing for random text entry, hiding all the entries in the drop-down list that don't star with the text that has already been entered.

I don't want these features. I want a select box that behaves pretty much exactly like one would expect a normal select box would in vanilla html.

I do want it bound to a data store, and I do want all the other extjs configuration goodies that come with the combo box. I just don't want users/testers freaking out when they encounter a select box that breaks their existing mental paradigm of how these things work.

So how can I get an extjs combo box to act more like a select box? Or am I using the wrong widget altogether?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光瘦了 2024-08-25 16:56:43

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

You can get that behaviour just by using the proper configuration when you instantiate the Ext.form.ComboBox object:

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); 

Replace the mode: 'local' and store argument in your case if you'd like it to be bound to a Ext.data.JsonStore for example.

眸中客 2024-08-25 16:56:43

目前接受的解决方案效果很好,但如果有人想要一个像普通 HTML 选择框一样处理键盘输入的 ComboBox(例如,每次按“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));
            }
        }
    }
}

The currently accepted solution works great, but if anyone wants a ComboBox that also handles keyboard input like a plain HTML select box (e.g., each time you press "P" is selects the next item in the list beginning with "P"), the following might be helpful:

{
    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));
            }
        }
    }
}
执笏见 2024-08-25 16:56:43

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

var combo = new Ext.form.ComboBox({
    typeAhead: false,

    ...

});

Did you try typeAhead = false? Not too sure if this is close to what you want.

var combo = new Ext.form.ComboBox({
    typeAhead: false,

    ...

});
预谋 2024-08-25 16:56:43
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
    }
 });
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
    }
 });
御弟哥哥 2024-08-25 16:56:43

只需使用 Ext.merge 函数

来自文档:http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-merge

Just use Ext.merge function

From the doc: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-merge

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文