Dojo Select onChange event firing when changing value programmatically

这一生的挚爱 提交于 2019-11-28 00:00:00

问题


I have a dojo (dijit) select dropdown that calls a js function onChange. I was expecting this to only call the onChange function when the user changes the value in the dropdown, however, it even calls the onChange function when I programmatically change the value of the dropdown from js code. How do I get it to only call the function when the user changes the dropdown value? It shouldn't call the function when I programmatically change the value.

<select jsId="ddlBoundaryType" id="ddlBoundaryType" name="ddlBoundaryType" 
                            dojoType="dijit.form.Select">
                            <option value="0">Circle</option>
                            <option value="1">Polygon</option>
                        </select>

dojo.addOnLoad(InitBoundaries);
    function InitBoundaries() {
        dojo.connect(dijit.byId("ddlBoundaryType"), 'onChange', Boundaries_ChangeBoundaryType); 
    }

回答1:


I think the proper fix in this case for you would be http://bugs.dojotoolkit.org/ticket/10594, since it deals directly with dijit.form.Select. Of course, there are a few ways to fix this.

  1. Upgrade dojo :).
  2. Inherit dijit.form.Select and "patch" the _updateSelection function.
  3. Extend dijit.form.Select and "patch" it directly there.

I will forgo the first. The the second and the third method are similar, so I will just post a simple fix using the third way,

dijit.form.Select.extend({
   _updateSelection: function() {
        this.value = this._getValueFromOpts();
        var val = this.value;
        if(!dojo.isArray(val)){
            val = [val];
        }
        if(val && val[0]){
            dojo.forEach(this._getChildren(), function(child){
                var isSelected = dojo.some(val, function(v){
                    return child.option && (v === child.option.value);
                });
                dojo.toggleClass(child.domNode, this.baseClass + "SelectedOption", isSelected);
                dijit.setWaiState(child.domNode, "selected", isSelected);
            }, this);
        }
   }
});

Note that I did not write this function, I happily plagiarized it from the source code with the last line, this._handleOnChange(this.value) removed.

myWidget.attr('value', newValue, false) // should now work without firing onChange.



回答2:


Often people solve this by using the priorityChange flag:

myWidget.set("value", 1234, false);

That will solve your problem except for subtle issues where the value is originally 123, you set it programatically to 456, and then the user sets it back to 123, in which case there won't be an onChange() event for the user action either.

For that reason you can additionally do:

myWidget._lastValueReported=null;



回答3:


A much simpler way is that, I would like to suggest the "_onChangeActive" flag, which is not recommended to be used, as it is serves internal purpose. But, if required, we could use it. "_onChangeActive" is a flag present in dojo Select type widgets, which is by default set to true. If this flag is set to true, the onChange event is triggered as usual. But, when it is set to false, the onChange event is not triggered, when the value is changed either by user or programatically.

e.g:

var widget = registry.byId('widget_id');
widget.set('_onChangeActive', false); // setting the flag to false
...//make changes to the select programatically
widget.set('_onChangeActive',true); // setting back to true after programatic changes are done

No need to inherit the existing dojo functionality for this or use separate external flags. The widget has one inbuilt for this - "_onChangeActive".



来源:https://stackoverflow.com/questions/3229747/dojo-select-onchange-event-firing-when-changing-value-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!