简体   繁体   English

组合框ExtJs的有害行为

[英]unwanted behavior of combo box ExtJs

There combo box and the associated Store, if there is no entry in the Store of the value entered by the user resets everything is correct but there is one unpleasant feature if the user enters a value that is at Store but he will do it quickly while storage has not had time to load the input value will be reset. 有组合框和关联的商店,如果在商店中没有用户输入的值的重置,则一切都正确,但是如果用户输入商店中的值,则有一个令人不快的功能,但他会在存储尚未加载时间,输入值将被重置。

How not reset the value entered by the user when he moves to another form field if the user did not wait until the Store load (if the entered value is in Store) 如果用户没有等到存储加载后(如果输入的值在存储区中),如何在用户移动到另一个表单字段时不重置用户输入的值

var bik = new Ext.form.ComboBox({
        store: storeBik,
        displayField: 'BANK_NAME',
        fieldLabel: 'БИК',
        name: 'BIK',
        hiddenName: 'BIK',
        valueField:'BIK',
        typeAhead: true,
        forceSelection:true,
        selectOnFocus:true,
        triggerAction: 'all',
        minChars : 1,
        mode: 'remote'
        resizable : true,
        validator : validBik,
        tpl: new Ext.XTemplate('<tpl for="."><div class="x-combo-list-item"><b>{BIK} </b> {BANK}</div></tpl>')

    });

The reason why this is happening is because you have turned on forceSelection . 发生这种情况的原因是因为您已打开forceSelection After blur ComboBox is trying to find suitable record in store for typed value. 在模糊之后, ComboBox试图在存储中为键入的值找到合适的记录。 If such record doesn't exist it resets value. 如果这样的记录不存在,它将重置值。

I can think off 2 solutions: 我可以考虑两种解决方案:

  • turn off forceSelection 关闭forceSelection
  • extend ComboBox 扩展ComboBox

I see that you have validBik validator attached. 我看到您已经附加了validBik验证器。 If you can validate value on client side, then turn off forceSelection and you have all you need. 如果您可以在客户端验证值,则关闭forceSelection ,即可满足需要。 If on the other hand you really need to have data in store to choose value from, then you should extend ComboBox . 另一方面,如果您确实需要存储数据以从中选择值,则应该扩展ComboBox

Below is ComboBox modification which holds value until request ends. 下面是ComboBox修改,它将保留值,直到请求结束。 It is not perfect, but maybe it will help you: 它不是完美的,但是也许可以帮助您:

var bik = new Ext.form.ComboBox({
    [...],

    // Check if query is queued or in progress
    isLoading: function() {
        return this.isStoreLoading || // check if store is making ajax request
            this.isQueryPending; // check if there is any query pending
    },

    // This is responsible for finding matching record in store
    assertValue: function() {
        if (this.isLoading()) {
            this.assertionRequired = true;
            return;
        }

        Ext.form.ComboBox.prototype.assertValue.apply(this, arguments);
    },

    // this is private method; you can equally write 'beforeload' event handler for store
    onBeforeLoad: function(){
        this.isQueryPending = false;
        this.isStoreLoading = true;

        Ext.form.ComboBox.prototype.onBeforeLoad.apply(this, arguments);
    },

    // catch moment when query is added to queue
    onKeyUp: function(e){
        var k = e.getKey();
        if(this.editable !== false && this.readOnly !== true && (k == e.BACKSPACE || !e.isSpecialKey())){
            this.isQueryPending = true;
        }

        Ext.form.ComboBox.prototype.onKeyUp.apply(this, arguments);
    },

    // this is private method; you can equally write 'load' event handler for store
    onLoad: function() {
        Ext.form.ComboBox.prototype.onLoad.apply(this, arguments);

        this.isQueryPending = false;
        this.isStoreLoading = false;
        if (this.assertionRequired === true) {
            delete this.assertionRequired;
            this.assertValue();
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM