简体   繁体   English

ExtJS:复选框单击或键入事件

[英]ExtJS: checkbox click or keyup event

Now I can handle change event of a checkbox in my controller . 现在,我可以在controller处理checkbox change事件。 I do it like so: 我这样做是这样的:

"[itemId=TestCheckbox]": {
   change: this.updateCheckbox
}

What I do not like about this, is that this event fires even when the state of the checkbox is changed programmatically - when, for example, the value is bound at the first time. 我对此不满意的是,即使以编程方式更改了复选框的状态(例如,第一次绑定该值时),也会触发此事件。 All this results in unnecessary server requests: 所有这些导致不必要的服务器请求:

  1. First request gets data from the server and binds the value to the checkbox 第一个请求从服务器获取数据,并将值绑定到复选框
  2. Change event is fired and another server request is done 触发更改事件,并且另一个服务器请求已完成

I do not like this. 我不喜欢这个。 I want something like keyup event, or check event, so that I could trigger server request, only when the state of the checkbox is changed by hand and not programmatically. 我想要诸如keyup事件或check事件之类的东西,以便仅当手动更改复选框的状态而不是通过编程更改状态时,才可以触发服务器请求。

You could try adding a mousedown/keydown handler to the checkbox el , then setting a flag there, and check for that flag in the change event handler 您可以尝试将mousedown/keydown处理程序添加到复选框el ,然后在其中设置一个标志,然后在change事件处理程序中检查该标志

{
    xtype:     'checkboxfield',
    listeners: {
        mousedown: {
            element: 'el',
            fn:      function (e, el) {
                Ext.getCmp(e.currentTarget.id).myFlag = true
            }
        },
        change:    function (cb, newValue, oldValue, eOpts) {
            console.log(cb, cb.myFlag)
        }

    }
}

If you are using setValue to set value to combo, it fires change event . 如果您正在使用setValue将值设置为combo,则会触发change event。 Check here 在这里检查

You can use suspendEvents and resumeEvents to avoid this. 您可以使用suspendEventsresumeEvents避免这种情况。

Call suspendEvents before setting value and call resumeEvents(true) after setting value. 在设置值之前调用suspendEvents,在设置值之后调用resumeEvents(true)

suspendEvents will bloack all events fired by setValue and resumeEvents will agaib resume firing events . suspendEvents将覆盖由setValue触发的所有事件, resumeEvents将再次恢复触发事件。 Don't forget to pass true argument to resumeEvents 不要忘记将true参数传递给resumeEvents

combo.suspendEvents();
combo.setValue(value);
combo.resumeEvents(true);

Read more about suspendEvents and resumeEvents . 阅读更多有关suspendEventsresumeEvents的信息

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

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