简体   繁体   English

Flex Combobox preChange事件

[英]Flex Combobox preChange event

I have a project in which i need to pop up an alert to the user before a combobox value is changed. 我有一个项目,需要在更改组合框值之前向用户弹出警报。 This feature is to allow the user to stay in current state if modifications were not saved. 如果未保存修改,此功能将允许用户保持当前状态。 Meaning that the user will be able to cancel the change. 这意味着用户将能够取消更改。

I have sub classed ComboBox and tried to hook on ITEM_CLICK of ComboBox.dropdown but this event is triggered after the value is changed. 我对ComboBox进行了子类化,并尝试挂接ComboBox.dropdown的ITEM_CLICK,但更改该值后会触发此事件。 Also, I've tried MOUSE_CLICK ans MOUSE_DOWN but without success. 另外,我尝试了MOUSE_CLICK和MOUSE_DOWN,但没有成功。

In my code, I have added a "preChange" event to my CustomComboBox. 在我的代码中,我向CustomComboBox添加了“ preChange”事件。 This event should be triggered before a change is made. 进行更改之前应触发此事件。 Also, I've introduced a method called commitChange that will be called manually to actually commit the change. 另外,我介绍了一种称为commitChange的方法,该方法将被手动调用以实际提交更改。

How can I achieve the desired result? 如何获得理想的结果?

This is how I've done it: 这是我的方法:

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onComplete();">
    <mx:Script>
        <![CDATA[
            import mx.events.CloseEvent;
            import mx.controls.Alert;
            import mx.events.ListEvent;
            private function onComplete():void {
                addEventListener(ListEvent.CHANGE, onChange);
                persistLastIndex();
            }

            private var _lastIndex:Number = 0;

            private function persistLastIndex():void {
                _lastIndex = selectedIndex;
            }

            private function onChange(event:ListEvent):void {
                Alert.show("Are you sure you want to change the selection?", "", Alert.YES|Alert.NO, null, onAlertClicked);
            }

            private function onAlertClicked(event:CloseEvent):void {
                if ( event.detail == Alert.NO ) {
                    selectedIndex = _lastIndex;
                } else {
                    _lastIndex = selectedIndex;
                }
            }

        ]]>
    </mx:Script>
</mx:ComboBox>

And to call it: 并称之为:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:rad="uk.co.rad.*">
    <rad:MyComboBox>
        <rad:dataProvider>
            <mx:Array>
                <mx:Object data="1" label="Value 1" />
                <mx:Object data="2" label="Value 2" />
            </mx:Array>
        </rad:dataProvider>
    </rad:MyComboBox>
</mx:Application>

Hope this helps. 希望这可以帮助。

Basically I needed an event before the change is made. 基本上,在进行更改之前,我需要一个事件。 I guess that'll do for now thought ... 我想现在就可以了...

I did modify your code to make it fire the preChange event when a change occur, stop the event propagation, and only after the change is really made fire the CHANGE event .. 我确实修改了您的代码,以使其在发生更改时触发preChange事件,停止事件传播,并且只有在真正使更改发生后才触发CHANGE事件..

Thanks for your prompt answer. 感谢您的迅速答复。

The easiest way to do it is to add two listeners to Event.CHANGE on the ComboBox, then set the priority of the handler you want to fire prior to the change as a negative value. 最简单的方法是在ComboBox的Event.CHANGE上添加两个侦听器,然后将要在更改之前触发的处理程序的优先级设置为负值。 For example: 例如:

myComboBox.addEventListener(Event.CHANGE, preChange, false, -100);
myComboBox.addEventListener(Event.CHANGE, postChange, false, 100);

The order of event dispatching: 事件调度的顺序:

  1. preChange(event:Event) preChange(event:Event)
  2. inherited change events (automatically have a default priority of 0) 继承的更改事件(自动具有默认优先级0)
  3. postChange(event:Event) postChange(event:Event)

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

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