2011-03-03 81 views
0

我有一個組合框可以作爲搜索應用程序的自我暗示。搜索功能正在通過搜索按鈕觸發。我還希望在組合框中的項目是雙擊或單擊時觸發搜索功能。代碼:AS3,如何在組合框中選擇項目時觸發搜索功能?

//for triggering search function from combobox(search_complex) it will be 
    something like that but i am not sure 
    search_complex.addEventListener(Event.CHANGE, search); 
    search(event:Event):void{//something will come hereto use "selctedItem" to 
    trigger search function} 


//search function which is working fine by pressing search button 
bt_search.addEventListener(MouseEvent.CLICK, search); 
function search(MouseEvent):void{ 
currentUserbase = []; 
for (var n:int = 0; n<allUserbase.length; n++) 
{ 
for (var k:int = 0; k<allUserbase[n].complex.length; k++) 
{ 
if ((allUserbase[n].complex[k].value.toLowerCase() == 
search_complex.text.toLowerCase() || search_complex.text=="")) 
{ 
currentUserbase.push(allUserbase[n]); 
} 
} 
} 
updateList(); 
}//end search 

回答

0

我相信你是在正確的軌道上。嘗試:

search_complex.addEventListener(Event.CHANGE, search); 
bt_search.addEventListener(MouseEvent.CLICK, search); 

function search(event:Event):void 
{ 
    currentUserbase = []; 
    for (var n:int = 0; n<allUserbase.length; n++) 
    { 
    for (var k:int = 0; k<allUserbase[n].complex.length; k++) 
    { 
     if ((allUserbase[n].complex[k].value.toLowerCase() == search_complex.text.toLowerCase() || search_complex.text=="")) 
     { 
     currentUserbase.push(allUserbase[n]); 
     } 
    } 
    } 

    updateList(); 

}//end search 

你應該使用search_complex.selectedItem.labelsearch_complex.selectedItem.label取決於你需要使用哪個屬性能得到您的組合框中選定的項目。

+0

@jason,感謝回答,但如果和如何使用search_complex.selectedItem.label怎麼這樣,當我在組合框中選擇一個項目,就會觸發「搜索」功能 – hanna 2011-03-03 16:38:55

+0

@hanna,假設'search'是實際執行搜索的函數,只要單擊'bt_search'按鈕或從'search_complex combobox'中選擇一個項目,它就會自動調用。這就是我的例子中eventListeners的用處。至於何時使用'search_complex.selectedItem.label',我假設您會在需要的地方使用它來設置搜索條件。如果'updateList()'使用'currentUserbase'數組的內容執行搜索,則可以在'search()'中將'search_complex.selectedItem.label'添加到數組中。 – 2011-03-03 17:03:34

+0

@jason,感謝您在這裏幫助我,但我仍然對事物感到困惑,我實際上使用AutoComplete高級示例中的autocomple框在以下鏈接中:http://developer.yahoo.com/flash/astra-flash/ autocomplete/examples.html現在,當我使用search_complex.addEventListener(Event.CHANGE,搜索);它只會自動執行搜索下拉列表中的第一項內容,而不是用戶選擇的項目。 updateList只是在搜索功能完成時用結果更新另一個列表。 – hanna 2011-03-03 17:23:14

1

我不明白你到底想要什麼。

它是rue,你有一個搜索功能,這將正常工作。 現在你不需要每個事件單獨處理程序。對所有事件使用一個就足夠了。由於所有其他事件都從這個基類繼承,因此functionparameter使用「Event」類型。

檢查我的代碼。 CD是我的組合框。這個例子是寫在flex3

<mx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 
     [Bindable] private var arr:ArrayCollection = new ArrayCollection([ 
      {name:"Alexander"}, 
      {name:"Bernd"}, 
      {name:"Carl"} 
     ]); 

     private function init():void 
     { 
      cb.addEventListener(MouseEvent.CLICK,search); 
      cb.addEventListener(MouseEvent.DOUBLE_CLICK,search); 
      cb.addEventListener(Event.CHANGE,search); 
     } 

     private function search (event:Event) :void 
     { 
      trace (event.type); 
     } 
    ]]> 
</mx:Script> 
相關問題