简体   繁体   English

如何在Ext JS中侦听用户编辑而不是文本字段的程序编辑?

[英]How to listen for user edits but not programmatic edits for textfield in Ext JS?

I added a listener to the change event on a TextField . 我在TextFieldchange事件中添加了一个侦听器。 When the user changes the value in the text field, my listener gets control. 当用户更改文本字段中的值时,我的侦听器将获得控制权。 This is great. 这很棒。

In some cases, I want to restore the old value in the listener. 在某些情况下,我想在侦听器中还原旧值。 However, when I call the setValue method in the listener, the change event fires again. 但是,当我在侦听器中调用setValue方法时, change事件再次触发。 What is the best way to restore the old value programmatically without the event firing again? 以编程方式恢复旧值而无需再次触发事件的最佳方法是什么?

You can set flag on TextField indicating that you change value programatically. 您可以在TextField上设置标志,以指示您以编程方式更改值。 Eg: 例如:

new Ext.form.TextField({
    renderTo: 'container',
    listeners: {
        change: function(sender, newValue, oldValue) {
            if (this.restoringValue) {
                return;
            }
            if (newValue == 'XXX') {
                this.restoringValue = true;
                this.setValue(oldValue);
                delete this.restoringValue;
            }
        }
    },
    value: 'XXX'
});

If you don't have any buffers or delays on change listener it should work. 如果您在更改侦听器上没有任何缓冲区或延迟,则它应该可以工作。

I found a solution to stop the check change event that is a property on the field that the checkChange event looks for. 我找到了一种解决方案来停止检查更改事件,该事件是checkChange事件查找字段上的一个属性。 it's 它的

field.suspendCheckChange = true field.suspendCheckChange = true

this is because the checkChange function that is fired by the setValue method is 这是因为setValue方法触发的checkChange函数是

checkChange: function() {
        var me = this,
            newVal, oldVal;

        if (!me.suspendCheckChange) {
            newVal = me.getValue();
            oldVal = me.lastValue;

            if (!me.isDestroyed && me.didValueChange(newVal, oldVal)) {
                me.lastValue = newVal;
                me.fireEvent('change', me, newVal, oldVal);
                me.onChange(newVal, oldVal);
            }
        }
    },

this is in 5.1, not sure if it's earlier versions.. 这是5.1版本,不确定是否是较早版本。

http://docs.sencha.com/extjs/5.1/5.1.1-apidocs/source/Field.html#Ext-form-field-Field-method-checkChange http://docs.sencha.com/extjs/5.1/5.1.1-apidocs/source/Field.html#Ext-form-field-Field-method-checkChange

Edit the event handler to look at the caller of the function. 编辑事件处理程序以查看函数的caller Based on that, drop out of the event handler. 基于此,退出事件处理程序。

See: How to get the caller event and dom element id 请参阅: 如何获取呼叫者事件和dom元素ID

setRawValue( Object value ) : Object setRawValue(Object value):对象

Sets the field's raw value directly, bypassing value conversion, change detection, and validation. 直接设置字段的原始值,绕过值转换,更改检测和验证。 http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.field.Base-method-setRawValue http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.field.Base-method-setRawValue

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

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