如何阻止 Accordion segment 切換?
昨天被問到:如何在一個 Accordion segment 切換前攔截下來,做些檢查,然後才允許它繼續切換?
例如:Accordion 裏有三個 segments, A, B, C,希望從 A 切換到 C 前先檢查 A 的內容是否正確,然後才允許切過去。
原本以為應該可以偵聽 Accordion 裏面的 change 事件,然後透過 event.preventDefault() 達到攔截的目地,但很遺憾這招不成功。
所以只好走上繼承再改寫的路。
基本上是 override selectedIndex 後動手腳即可,quick and easy
Actionscript:
-
public var isDirty:Boolean = true;
-
-
//user wanted to swtich to this index
-
private var targetIndex:int;
-
-
override public function set selectedIndex(value:int):void{
-
-
if( isDirty ){
-
targetIndex = value;
-
Alert.show( "do you want to save ?", "Save or Not ?", Alert.YES|Alert.NO, null, onClose );
-
-
}else{
-
super.selectedIndex = value;
-
}
-
-
}
-
-
function onClose( evt:CloseEvent ){
-
if( evt.detail == Alert.YES ){
-
trace("you should press save button in current screen");
-
}else{
-
isDirty = false;
-
this.selectedIndex = targetIndex;
-
}
-
}
改好的檔案+範例
(注意這是用 Flex Builder 3 Beta2 製作)
by admin


Trackback this post | Subscribe to the comments via RSS Feed