如何阻止 Accordion segment 切換?

In General   November 30, 2007 - 10:10 am

昨天被問到:如何在一個 Accordion segment 切換前攔截下來,做些檢查,然後才允許它繼續切換?

例如:Accordion 裏有三個 segments, A, B, C,希望從 A 切換到 C 前先檢查 A 的內容是否正確,然後才允許切過去。

原本以為應該可以偵聽 Accordion 裏面的 change 事件,然後透過 event.preventDefault() 達到攔截的目地,但很遺憾這招不成功。

所以只好走上繼承再改寫的路。

基本上是 override selectedIndex 後動手腳即可,quick and easy :)

Actionscript:
  1. public var isDirty:Boolean = true;
  2.  
  3. //user wanted to swtich to this index
  4. private var targetIndex:int;
  5.  
  6. override public function set selectedIndex(value:int):void{
  7.    
  8.     if( isDirty ){
  9.         targetIndex = value;
  10.         Alert.show( "do you want to save ?", "Save or Not ?", Alert.YES|Alert.NO, null, onClose );
  11.        
  12.     }else{
  13.         super.selectedIndex  = value;
  14.     }
  15.    
  16. }
  17.  
  18. function onClose( evt:CloseEvent ){
  19.     if( evt.detail == Alert.YES ){
  20.         trace("you should press save button in current screen");
  21.     }else{
  22.         isDirty = false;
  23.         this.selectedIndex = targetIndex;
  24.     }
  25. }

改好的檔案+範例
(注意這是用 Flex Builder 3 Beta2 製作)

by admin

留言回應

hidden

您的留言會先經過站長認証後才刊登在網站上。
your comments will be approved by Administrator before appearing on the page.

Trackback this post  |  Subscribe to the comments via RSS Feed

mobile phone