2010-09-07 59 views
0

我有一个应用程序,用户在标题窗口弹出窗口中将所需信息放入文本输入中,并将输入的数据发送到数据网格中。这是我第一次调用弹出窗口并输入信息时,它工作正常。问题出在这里:下一次我调用组件弹出并弹出弹出窗口上的任何位置(包括textInputs)时,数组中的第一项会自动输入到textInput中,用户应该自己输入数据(顺便说一下,这个textInput也是一个自动完成的组件)。除非用户自己输入数据,否则我想要的只是输入textinput。我一直在调整它几个小时,并陷入困境。任何建议将不胜感激。组件在需要之前从数组中获取项目

下面的代码: (这是主页)

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    creationComplete="init()" > 

<mx:Script> 
    <![CDATA[ 
     import mx.managers.PopUpManager; 
      import mx.collections.ArrayCollection; 
      import mx.core.IFlexDisplayObject; 
      import com.AddTrade; 

      [Bindable]public var dgItems:ArrayCollection = new ArrayCollection(); 

      private var addTradeScreen:AddTrade; 



private function init():void 
{ 
    addTradeScreen = new AddTrade(); 
    addTradeScreen.addEventListener("SaveTrade", saveTrade); 
} 

private function addTrade():void 
{ 
    PopUpManager.addPopUp(addTradeScreen, this, true); 
    PopUpManager.centerPopUp(addTradeScreen); 
    addTradeScreen.country.text = ""; 
    addTradeScreen.Notes.text =""; 
    addTradeScreen.myDate.text=""; 


} 

private function saveTrade(e:Event):void 
{ 
    var trade:Trade= new Trade(); 
    trade.country = addTradeScreen.country.text; 
    trade.myDate = addTradeScreen.myDate.text; 
    trade.direction = addTradeScreen.combo.text; 
    dgItems.addItem(trade); 
    PopUpManager.removePopUp(addTradeScreen); 
} 

    public function deleteItem(event:MouseEvent):void 
        { 
         dgItems.removeItemAt(myDG.selectedIndex); 
         dgItems.refresh(); 
        } 


    public function myColStyleFunc(data:Object,col:AdvancedDataGridColumn):Object 
       { 
        if(data["myProfit"]<0) 
        { 
         return {color:0xFF0000}; 
        } 
        else 
        { 
         return null; 
        } 
       } 



     ]]> 
    </mx:Script> 

<mx:AdvancedDataGrid x="10" y="10" dataProvider="{dgItems}" id="myDG" variableRowHeight="true" wordWrap="true" tabIndex="12" editable="false" height="429" width="942" doubleClickEnabled="true"> 
    <mx:columns> 
     <mx:AdvancedDataGridColumn width="120" headerText="Country:" dataField="country"/> 
     <mx:AdvancedDataGridColumn width="120" headerText="Date:" dataField="myDate"/>   
     <mx:AdvancedDataGridColumn width="160" headerText="Notes:" dataField="tradeNotes"/> 
     <mx:AdvancedDataGridColumn width="129" headerText="Delete" > 
      <mx:itemRenderer> 
       <mx:Component id="deleteButton" > 
      <mx:Image click="outerDocument.deleteItem(event)" buttonMode="true" source="@Embed(source='Assets/trash2.png')" scaleContent="false" verticalAlign="middle" horizontalAlign="center" width="30" height="30"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:AdvancedDataGridColumn> 
    </mx:columns> 
</mx:AdvancedDataGrid> 
<mx:Button x="733" y="447" click="addTrade();" label="Add New Trade" height="36" width="219" fontSize="14"/> 

这里的弹出组件代码:

    <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" 
        layout="absolute" width="422" height="294" 
        title="Log Ur Trade" showCloseButton="true" close="close(event)" 
        xmlns:comp="com.adobe.flex.extras.controls.*" 
        initialize="data1 = new ArrayCollection(countries); 
      data2 = new ArrayCollection(countries);"> 


        <mx:Metadata> 
     [Event(name="SaveTrade")] 
     </mx:Metadata> 

     <mx:Script> 
      <![CDATA[ 
       import mx.managers.PopUpManager; 
       import mx.events.CloseEvent; 
       import mx.events.FlexEvent; 
       import mx.collections.ArrayCollection; 



     [Bindable] private var data1: ArrayCollection; 
     [Bindable] private var data2: ArrayCollection; 



     [Bindable]   
     public var countries: Array = [ 
             {id: 0, name:"Monaco"}, 
             {id: 1, name:"Macau"}, 
             {id: 2, name:"Hong Kong"}, 
             {id: 3, name:"Singapore"}, 
             {id: 4, name:"Gibraltar"}, 
             {id: 5, name:"Vatican City"}, 
             {id: 6, name:"Malta"}, 
             {id: 7, name:"Bermuda"}, 
             {id: 8, name:"Maldives"}, 
             {id: 9, name:"Bahrain"}, 
             {id: 10, name:"Bangladesh"}, 
             {id: 11, name:"Channel Islands"}, 
             {id: 12, name:"Nauru"}, 
             {id: 13, name:"Taiwan"}, 
             {id: 14, name:"Barbados"}, 
             {id: 15, name:"Palestinian territories"}, 
             {id: 16, name:"Mauritius"}, 
             {id: 17, name:"Aruba"}, 
             {id: 18, name:"South Korea"}, 
             {id: 19, name:"San Marino"}, 
             {id: 20, name:"Puerto Rico"}, 
             {id: 21, name:"Tuvalu"}, 
             {id: 22, name:"Netherlands"}, 
             {id: 23, name:"Martinique"}, 
             {id: 24, name:"Comoros"}, 
             {id: 25, name:"Lebanon"}, 
             {id: 26, name:"Rwanda"}, 
             {id: 27, name:"Marshall Islands"}, 
             {id: 28, name:"Belgium"}, 
             {id: 29, name:"Japan"}, 
             {id: 30, name:"India"}, 
             {id: 31, name:"El Salvador"}, 
             {id: 32, name:"American Samoa"}, 
             {id: 33, name:"U.S. Virgin Islands"}, 
             {id: 34, name:"Sri Lanka"}, 
             {id: 35, name:"R union"}, 
             {id: 36, name:"Guam"}, 
             {id: 37, name:"Haiti"}, 
             {id: 38, name:"Saint Vincent and the Grenadines"}, 
             {id: 39, name:"Israel"}, 
             {id: 40, name:"Saint Lucia"}, 
             {id: 41, name:"Philippines"}, 
             {id: 42, name:"Burundi"}, 
             {id: 43, name:"Guadeloupe"}, 
             {id: 44, name:"Grenada"}, 
             {id: 45, name:"Trinidad and Tobago"}, 
             {id: 46, name:"Vietnam"}, 
             {id: 47, name:"United Kingdom"}, 
             {id: 48, name:"Jamaica"}, 
             {id: 49, name:"Germany"}, 
             {id: 50, name:"Netherlands Antilles"}, 
             {id: 51, name:"Liechtenstein"}, 
             {id: 52, name:"Pakistan"}, 
             {id: 53, name:"Italy"}, 
             {id: 54, name:"North Korea"}, 
             {id: 55, name:"Nepal"}, 
             {id: 56, name:"Antigua and Barbuda"}, 
             {id: 57, name:"Dominican Republic"}, 
             {id: 58, name:"Luxembourg"}, 
             {id: 59, name:"Seychelles"}, 
             {id: 60, name:"Switzerland"}, 
             {id: 61, name:"Northern Mariana Islands"}, 
             {id: 62, name:"Cayman Islands"}, 
             {id: 63, name:"Saint Kitts and Nevis"}, 
             {id: 64, name:"Sao Tome and Principe"}, 
             {id: 65, name:"Federated States of Micronesia"}, 
             {id: 66, name:"Kuwait"}, 
             {id: 67, name:"British Virgin Islands"}, 
             {id: 68, name:"Andorra"}, 
             {id: 69, name:"Nigeria"}, 
             {id: 70, name:"People's Republic of China"}, 
             {id: 71, name:"Tonga"}, 
             {id: 72, name:"Kiribati"}, 
             {id: 73, name:"The Gambia"}, 
             {id: 74, name:"Anguilla"}, 
             {id: 75, name:"Isle of Man"}, 
             {id: 76, name:"Czech Republic"}, 
             {id: 77, name:"Denmark"}, 
             {id: 78, name:"Cape Verde"}, 
             {id: 79, name:"Thailand"}, 
             {id: 80, name:"Moldova"}, 
             {id: 81, name:"Uganda"}, 
             {id: 82, name:"Poland"}, 
             {id: 83, name:"Indonesia"}, 
             {id: 84, name:"Guatemala"}, 
             {id: 85, name:"Tokelau"}, 
             {id: 86, name:"Portugal"}, 
             {id: 87, name:"Slovakia"}, 
             {id: 88, name:"France (Metropolitan)"}, 
             {id: 89, name:"Albania"}, 
             {id: 90, name:"Malawi"}, 
             {id: 91, name:"Hungary"}, 
             {id: 92, name:"Togo"}, 
             {id: 93, name:"Serbia"}, 
             {id: 94, name:"Dominica"}, 
             {id: 95, name:"Syria"}, 
             {id: 96, name:"Cuba"}, 
             {id: 97, name:"Armenia"}, 
             {id: 98, name:"Austria"}, 
             {id: 99, name:"Azerbaijan"}, 
             {id: 100, name:"Slovenia"}, 
             {id: 101, name:"Turkey"}, 
             {id: 102, name:"Ghana"}, 
             {id: 103, name:"Romania"}, 
             {id: 104, name:"Cyprus"}, 
             {id: 105, name:"Spain"}, 
             {id: 106, name:"Costa Rica"}, 
             {id: 107, name:"Greece"}, 
             {id: 108, name:"Croatia"}, 
             {id: 109, name:"Republic of Macedonia"}, 
             {id: 110, name:"Cambodia"}, 
             {id: 111, name:"Wallis and Futuna"}, 
             {id: 112, name:"Sierra Leone"}, 
             {id: 113, name:"Ukraine"}, 
             {id: 114, name:"Malaysia"}, 
             {id: 115, name:"Bosnia and Herzegovina"}, 
             {id: 116, name:"Cook Islands"}, 
             {id: 117, name:"Benin"}, 
             {id: 118, name:"Myanmar"}, 
             {id: 119, name:"Egypt"}, 
             {id: 120, name:"Qatar"}, 
             {id: 121, name:"Morocco"}, 
             {id: 122, name:"Ethiopia"}, 
             {id: 123, name:"Bulgaria"}, 
             {id: 124, name:"Iraq"}, 
             {id: 125, name:"Samoa"}, 
             {id: 126, name:"Brunei"}, 
             {id: 127, name:"Honduras"}, 
             {id: 128, name:"Georgia"}, 
             {id: 129, name:"French Polynesia"}, 
             {id: 130, name:"Jordan"}, 
             {id: 131, name:"East Timor"}, 
             {id: 132, name:"Tunisia"}, 
             {id: 133, name:"Turks and Caicos Islands"}, 
             {id: 134, name:"Swaziland"}, 
             {id: 135, name:"Uzbekistan"}, 
             {id: 136, name:"Senegal"}, 
             {id: 137, name:"Lesotho"}, 
             {id: 138, name:"Ireland"}, 
             {id: 139, name:"Kenya"}, 
             {id: 140, name:"te d'Ivoire"}, 
             {id: 141, name:"Mexico"}, 
             {id: 142, name:"United Arab Emirates"}, 
             {id: 143, name:"Lithuania"}, 
             {id: 144, name:"Burkina Faso"}, 
             {id: 145, name:"Belarus"}, 
             {id: 146, name:"Ecuador"}, 
             {id: 147, name:"Fiji"}, 
             {id: 148, name:"Bhutan"}, 
             {id: 149, name:"Afghanistan"}, 
             {id: 150, name:"Tajikistan"}, 
             {id: 151, name:"Montenegro"}, 
             {id: 152, name:"Montserrat"}, 
             {id: 153, name:"Guinea-Bissau"}, 
             {id: 154, name:"Palau"}, 
             {id: 155, name:"Panama"}, 
             {id: 156, name:"Nicaragua"}, 
             {id: 157, name:"Iran"}, 
             {id: 158, name:"Tanzania"}, 
             {id: 159, name:"Saint Helena"}, 
             {id: 160, name:"Colombia"}, 
             {id: 161, name:"Yemen"}, 
             {id: 162, name:"South Africa"}, 
             {id: 163, name:"Guinea"}, 
             {id: 164, name:"Eritrea"}, 
             {id: 165, name:"Latvia"}, 
             {id: 166, name:"Cameroon"}, 
             {id: 167, name:"Djibouti"}, 
             {id: 168, name:"Faroe Islands"}, 
             {id: 169, name:"Zimbabwe"}, 
             {id: 170, name:"Madagascar"}, 
             {id: 171, name:"United States"}, 
             {id: 172, name:"Estonia"}, 
             {id: 173, name:"Liberia"}, 
             {id: 174, name:"Venezuela"}, 
             {id: 175, name:"Kyrgyzstan"}, 
             {id: 176, name:"Laos"}, 
             {id: 177, name:"Mozambique"}, 
             {id: 178, name:"Democratic Republic of the Congo"}, 
             {id: 179, name:"Saint-Pierre and Miquelon"}, 
             {id: 180, name:"The Bahamas"}, 
             {id: 181, name:"Brazil"}, 
             {id: 182, name:"Peru"}, 
             {id: 183, name:"Chile"}, 
             {id: 184, name:"Sweden"}, 
             {id: 185, name:"Uruguay"}, 
             {id: 186, name:"Equatorial Guinea"}, 
             {id: 187, name:"Vanuatu"}, 
             {id: 188, name:"Solomon Islands"}, 
             {id: 189, name:"Finland"}, 
             {id: 190, name:"Zambia"}, 
             {id: 191, name:"Paraguay"}, 
             {id: 192, name:"New Zealand"}, 
             {id: 193, name:"Sudan"}, 
             {id: 194, name:"Argentina"}, 
             {id: 195, name:"Algeria"}, 
             {id: 196, name:"Pitcairn Islands"}, 
             {id: 197, name:"Somalia"}, 
             {id: 198, name:"Angola"}, 
             {id: 199, name:"New Caledonia"}, 
             {id: 200, name:"Papua New Guinea"}, 
             {id: 201, name:"Norway"}, 
             {id: 202, name:"Belize"}, 
             {id: 203, name:"Republic of the Congo"}, 
             {id: 204, name:"Saudi Arabia"}, 
             {id: 205, name:"Niger"}, 
             {id: 206, name:"Mali"}, 
             {id: 207, name:"Turkmenistan"}, 
             {id: 208, name:"Russia"}, 
             {id: 209, name:"Bolivia"}, 
             {id: 210, name:"Oman"}, 
             {id: 211, name:"Chad"}, 
             {id: 212, name:"Central African Republic"}, 
             {id: 213, name:"Niue"}, 
             {id: 214, name:"Kazakhstan"}, 
             {id: 215, name:"Gabon"}, 
             {id: 216, name:"Guyana"}, 
             {id: 217, name:"Libya"}, 
             {id: 218, name:"Canada"}, 
             {id: 219, name:"Botswana"}, 
             {id: 220, name:"Mauritania"}, 
             {id: 221, name:"Iceland"}, 
             {id: 222, name:"Suriname"}, 
             {id: 223, name:"Australia"}, 
             {id: 224, name:"Namibia"}, 
             {id: 225, name:"French Guiana"}, 
             {id: 226, name:"Mongolia"}, 
             {id: 227, name:"Western Sahara"}, 
             {id: 228, name:"Falkland Islands"}, 
             {id: 229, name:"Greenland"} 
      ]; 


     private function save():void 
      { 
       this.dispatchEvent(new Event("SaveTrade")); 
      } 

     private function close(evt:CloseEvent):void 
      { 
       PopUpManager.removePopUp(this); 
      } 
      ]]> 
     </mx:Script> 


    <mx:Button x="197" y="203" label="Submit" width="114" id="submitTrade" click="save()" tabIndex="11"/>     
    <mx:TextArea x="12" y="118" height="107" id="Notes" tabIndex="10" fontStyle="normal"/> 
     <mx:Label x="10" y="97" text="Notes:" fontWeight="bold" height="18" fontFamily="Georgia" fontSize="12"/> 
      <mx:DateField x="83" y="40" width="89" height="20" id="myDate" tabIndex="2"/> 
       <mx:Label x="10" y="42" text="Date:" fontWeight="bold" height="20" width="87" fontFamily="Georgia"/> 
    <comp:AutoCompleteModified 
       width="80" 
       dataProvider="{data2}" 
       labelField="name" 
       x="83" y="68" height="21" id="country" tabIndexChange="2" tabIndex="3"/> 
      <mx:Label x="10" y="70" text="Instrument:" width="75" fontWeight="bold" fontFamily="Georgia"/> 
    <mx:ComboBox id = "combo" x="83" y="10" width="89" rowCount="2" tabIndex="1"> 
     <mx:ArrayCollection> 
       <mx:String>Long</mx:String> 
       <mx:String>Short</mx:String> 
     </mx:ArrayCollection> 
    </mx:ComboBox> 
      <mx:Label x="10" y="12" text="Direction:" width="65" fontWeight="bold" fontFamily="Georgia"/> 

最后的自动完成功能的代码组件(AutocompleteModified):

package com.adobe.flex.extras.controls 
{ 
    import flash.events.Event; 
    import flash.events.FocusEvent; 
    import flash.events.KeyboardEvent; 
    import flash.net.SharedObject; 
    import flash.ui.Keyboard; 

    import mx.collections.ListCollectionView; 
import mx.controls.ComboBox; 
import mx.core.UIComponent; 

[Event(name="typedTextChange", type="flash.events.Event")] 


public class AutoCompleteModified extends ComboBox 
    { 


public function AutoCompleteModified() 
{ 
    super(); 

    //Make ComboBox look like a normal text field 
    editable = true; 
    if(keepLocalHistory) 
     addEventListener("focusOut",focusOutHandler); 

    setStyle("arrowButtonWidth",0); 
    setStyle("fontWeight","normal"); 
    setStyle("cornerRadius",0); 
    setStyle("paddingLeft",0); 
    setStyle("paddingRight",0); 
    rowCount = 7; 
} 


private var cursorPosition:Number=0; 


private var prevIndex:Number = -1; 


private var removeHighlight:Boolean = false;  


private var showDropdown:Boolean=false; 


private var showingDropdown:Boolean=false; 


private var tempCollection:Object; 


private var usingLocalHistory:Boolean=false; 


private var dropdownClosed:Boolean=true; 


//---------------------------------- 
// editable 
//---------------------------------- 
/** 
* @private 
*/ 
override public function set editable(value:Boolean):void 
{ 
    //This is done to prevent user from resetting the value to false 
    super.editable = true; 
} 
/** 
* @private 
*/ 
override public function set dataProvider(value:Object):void 
{ 
    super.dataProvider = value; 
    if(!usingLocalHistory) 
     tempCollection = value; 
} 

//---------------------------------- 
// labelField 
//---------------------------------- 
/** 
* @private 
*/ 
override public function set labelField(value:String):void 
{ 
    super.labelField = value; 

    invalidateProperties(); 
    invalidateDisplayList(); 
} 


//-------------------------------------------------------------------------- 
// 
// Properties 
// 
//-------------------------------------------------------------------------- 


//---------------------------------- 
// filterFunction 
//---------------------------------- 

/** 
* @private 
* Storage for the filterFunction property. 
*/ 
private var _filterFunction:Function = defaultFilterFunction; 

/** 
* @private 
*/ 
private var filterFunctionChanged:Boolean = true; 

[Bindable("filterFunctionChange")] 
[Inspectable(category="General")] 

/** 
* A function that is used to select items that match the 
* function's criteria. 
* A filterFunction is expected to have the following signature: 
* 
* <pre>f(item:~~, text:String):Boolean</pre> 
* 
* where the return value is <code>true</code> if the specified item 
* should displayed as a suggestion. 
* Whenever there is a change in text in the AutoComplete control, this 
* filterFunction is run on each item in the <code>dataProvider</code>. 
* 
* <p>The default implementation for filterFunction works as follows:<br> 
* If "AB" has been typed, it will display all the items matching 
* "AB~~" (ABaa, ABcc, abAc etc.).</p> 
* 
* <p>An example usage of a customized filterFunction is when text typed 
* is a regular expression and we want to display all the 
* items which come in the set.</p> 
* 
* @example 
* <pre> 
* public function myFilterFunction(item:~~, text:String):Boolean 
* { 
*  public var regExp:RegExp = new RegExp(text,""); 
*  return regExp.test(item); 
* } 
* </pre> 
* 
*/ 
public function get filterFunction():Function 
{ 
    return _filterFunction; 
} 

/** 
* @private 
*/ 
public function set filterFunction(value:Function):void 
{ 
    //An empty filterFunction is allowed but not a null filterFunction 
    if(value!=null) 
    { 
     _filterFunction = value; 
     filterFunctionChanged = true; 

     invalidateProperties(); 
     invalidateDisplayList(); 

     dispatchEvent(new Event("filterFunctionChange")); 
    } 
    else 
     _filterFunction = defaultFilterFunction; 
} 

//---------------------------------- 
// filterFunction 
//---------------------------------- 

/** 
* @private 
* Storage for the keepLocalHistory property. 
*/ 
private var _keepLocalHistory:Boolean = false; 

/** 
* @private 
*/ 
private var keepLocalHistoryChanged:Boolean = true; 

[Bindable("keepLocalHistoryChange")] 
[Inspectable(category="General")] 

/** 
* When true, this causes the control to keep track of the 
* entries that are typed into the control, and saves the 
* history as a local shared object. When true, the 
* completionFunction and dataProvider are ignored. 
* 
* @default "false" 
*/ 
public function get keepLocalHistory():Boolean 
{ 
    return _keepLocalHistory; 
} 

/** 
* @private 
*/ 
public function set keepLocalHistory(value:Boolean):void 
{ 
    _keepLocalHistory = value; 
} 

//---------------------------------- 
// lookAhead 
//---------------------------------- 

/** 
* @private 
* Storage for the lookAhead property. 
*/ 
private var _lookAhead:Boolean=false; 

/** 
* @private 
*/ 
private var lookAheadChanged:Boolean; 

[Bindable("lookAheadChange")] 
[Inspectable(category="Data")] 

/** 
* lookAhead decides whether to auto complete the text in the text field 
* with the first item in the drop down list or not. 
* 
* @default "false" 
*/ 
public function get lookAhead():Boolean 
{ 
    return _lookAhead; 
} 

/** 
* @private 
*/ 
public function set lookAhead(value:Boolean):void 
{ 
    _lookAhead = value; 
    lookAheadChanged = true; 
} 

//---------------------------------- 
// typedText 
//---------------------------------- 

/** 
* @private 
* Storage for the typedText property. 
*/ 
private var _typedText:String=""; 
/** 
* @private 
*/ 
private var typedTextChanged:Boolean; 

[Bindable("typedTextChange")] 
[Inspectable(category="Data")] 

/** 
* A String to keep track of the text changed as 
* a result of user interaction. 
*/ 
public function get typedText():String 
{ 
    return _typedText; 
} 

/** 
* @private 
*/ 
public function set typedText(input:String):void 
{ 
    _typedText = input; 
    typedTextChanged = true; 

    invalidateProperties(); 
    invalidateDisplayList(); 
    dispatchEvent(new Event("typedTextChange")); 
} 


override protected function commitProperties():void 
{ 
    super.commitProperties(); 

    if(!dropdown) 
     selectedIndex=-1; 

    if(dropdown) 
    { 
     if(typedTextChanged) 
     { 
      cursorPosition = textInput.selectionBeginIndex; 

      updateDataProvider(); 

      //In case there are no suggestions there is no need to show the dropdown 
      if(collection.length==0 || typedText==""|| typedText==null) 
      { 
       dropdownClosed=true; 
       showDropdown=false; 
      } 
      else 
      { 
       showDropdown = true; 
       selectedIndex = 0; 
      } 
     } 
    } 
} 


override protected function focusOutHandler(event:FocusEvent):void 
{ 
    super.focusOutHandler(event) 
    if(keepLocalHistory && dataProvider.length==0) 
     addToLocalHistory(); 
} 

override public function getStyle(styleProp:String):* 
{ 
    if(styleProp != "openDuration") 
     return super.getStyle(styleProp); 
    else 
    { 
     if(dropdownClosed) 
      return super.getStyle(styleProp); 
     else 
      return 0; 
    } 
} 

override protected function keyDownHandler(event:KeyboardEvent):void 
{ 
    super.keyDownHandler(event); 

    if(!event.ctrlKey) 
    { 
     //An UP "keydown" event on the top-most item in the drop-down 
     //or an ESCAPE "keydown" event should change the text in text 
     // field to original text 
     if(event.keyCode == Keyboard.UP && prevIndex==0) 
     { 
      textInput.text = _typedText; 
      textInput.setSelection(textInput.text.length, textInput.text.length); 
      selectedIndex = -1; 
     } 
     else if(event.keyCode==Keyboard.ESCAPE && showingDropdown) 
     { 
      textInput.text = _typedText; 
      textInput.setSelection(textInput.text.length, textInput.text.length); 
      showingDropdown = false; 
      dropdownClosed=true; 
     } 
     else if(event.keyCode == Keyboard.ENTER) 
     { 
      if(keepLocalHistory && dataProvider.length==0) 
       addToLocalHistory(); 



      /* 
      textInput.text = selectedLabel; 
      textInput.setSelection(cursorPosition, textInput.text.length); 
      textInput.setSelection(textInput.text.length,_typedText.length); 
      */ 

     } 
     else if(lookAhead && event.keyCode == Keyboard.BACKSPACE 
     || event.keyCode == Keyboard.DELETE) 
      removeHighlight = true; 
    } 
    else 
     if(event.ctrlKey && event.keyCode == Keyboard.UP) 
      dropdownClosed=true; 

    prevIndex = selectedIndex; 
} 

override protected function measure():void 
{ 
    super.measure(); 
    measuredWidth = mx.core.UIComponent.DEFAULT_MEASURED_WIDTH; 
} 

override protected function updateDisplayList(unscaledWidth:Number, 
          unscaledHeight:Number):void 
{ 

    super.updateDisplayList(unscaledWidth, unscaledHeight); 

    //An UP "keydown" event on the top-most item in the drop 
    //down list otherwise changes the text in the text field to "" 
    if(selectedIndex == -1) 
     textInput.text = typedText; 

    if(dropdown) 
    { 
     if(typedTextChanged) 
     { 
      //This is needed because a call to super.updateDisplayList() set the text 
      // in the textInput to "" and thus the value 
      //typed by the user losts 
      if(lookAhead && showDropdown && typedText!="" && !removeHighlight) 
      { 
       var label:String = itemToLabel(collection[0]); 
       var index:Number = label.toLowerCase().indexOf(_typedText.toLowerCase()); 
       if(index==0) 
       { 
        textInput.text = _typedText+label.substr(_typedText.length); 
        textInput.setSelection(textInput.text.length,_typedText.length); 
       } 
       else 
       { 
        textInput.text = _typedText; 
        textInput.setSelection(cursorPosition, cursorPosition); 
        removeHighlight = false; 
       } 

      } 
      else 
      { 
       textInput.text = _typedText; 
       textInput.setSelection(cursorPosition, cursorPosition); 
       removeHighlight = false; 
      } 

      typedTextChanged= false; 
     } 
     else if(typedText) 

      textInput.setSelection(_typedText.length,textInput.text.length); 
    } 
    if(showDropdown && !dropdown.visible) 
    { 

     super.open(); 
     showDropdown = false; 
     showingDropdown = true; 

     if(dropdownClosed) 
      dropdownClosed=false; 
    } 
} 



override protected function textInput_changeHandler(event:Event):void 
{ 
    super.textInput_changeHandler(event); 
    //Stores the text typed by the user in a variable 
    typedText=text; 
} 

private function addToLocalHistory():void 
{ 
    if (id != null && id != "" && text != null && text != "") 
    { 
     var so:SharedObject = SharedObject.getLocal("AutoCompleteData"); 

     var savedData : Array = so.data.suggestions; 
     //No shared object has been created so far 
     if (savedData == null) 
      savedData = new Array(); 

     var i:Number=0; 
     var flag:Boolean=false; 
     //Check if this entry is there in the previously saved shared object data 
     for(i=0;i<savedData.length;i++) 
      if(savedData[i]==text) 
      { 
       flag=true; 
       break; 
      } 
     if(!flag) 
     { 
      //Also ensure it is not there in the dataProvider 
      for(i=0;i<collection.length;i++) 
       if(defaultFilterFunction(itemToLabel(ListCollectionView(collection).getItemAt(i)),text)) 
       { 
        flag=true; 
        break; 
       } 
     } 
     if(!flag) 
      savedData.push(text); 

     so.data.suggestions = savedData; 
     //write the shared object in the .sol file 
     so.flush(); 
    } 
} 

private function defaultFilterFunction(element:*, text:String):Boolean 
{ 
    var label:String = itemToLabel(element); 
    return (label.toLowerCase().substring(0,text.length) == text.toLowerCase()); 
} 

private function templateFilterFunction(element:*):Boolean 
{ 
    var flag:Boolean=false; 
    if(filterFunction!=null) 
     flag=filterFunction(element,typedText); 
    return flag; 
} 


private function updateDataProvider():void 
{ 
    dataProvider = tempCollection; 
    collection.filterFunction = templateFilterFunction; 
    collection.refresh(); 


    if(collection.length==0 && keepLocalHistory) 
    { 
     var so:SharedObject = SharedObject.getLocal("AutoCompleteData"); 
     usingLocalHistory = true; 
     dataProvider = so.data.suggestions; 
     usingLocalHistory = false; 
     collection.filterFunction = templateFilterFunction; 
     collection.refresh(); 
    } 
} 

//-------------------------------------------------------------------------- 
// 
// modified source 
// 
//-------------------------------------------------------------------------- 

/** 
* Closes the combox and set the selection which is lost using Flex 3 
* 
* @event Event Trigger event to close the combobox 
*/  
override public function close(event:Event = null):void 
{ 
    super.close(event); 

    if(selectedIndex == 0) 
    { 
     // set the text using the selected label 
     textInput.text = selectedLabel; 
     // select the text from typed text position to texts length 
     textInput.setSelection(cursorPosition, textInput.text.length);  
    } 

} 

}

}

+0

你想做什么,请详细说明。 – Eugene 2010-09-07 23:04:57

回答

1

这可能的ComboBox机制的dataProvider每次更改后设置其selectedIndex属性设置为0的结果。你必须在你的代码的某个地方将它重新设置为-1(比如在popup的creationComplete处理程序中)。