Spark Combox with case-in-sensitive search

The Spark ComboBox control also searches the item list as the user enters characters into the prompt area. As the user enters characters, the drop-down area of the control opens. It then and scrolls to and highlights the closest match in the item list.

itemMatchingFunction is a callback function used to search the item list as the user enters characters into the prompt area. The function referenced by this property takes an input string and return the index of the items in the data provider that match the input. The items are returned as a Vector.<int> of indices in the data provider.

The callback function must have the following signature:
     * function myMatchingFunction(comboBox:ComboBox, inputText:String):Vector.&lt;int&gt;</pre>
    By default, if an input string of length n is equivalent to the first n characters of an item (ignoring case), then it is a match to that item. For example, ‘aRiz’ is a match to “Arizona” while ‘riz’ is not.</p>

    We have used this function to make case-in-sensitive Combobox.

CaseSensComboBox

<local:CaseSensComboBox caseSens=”true”/>

 

CaseSensComboBox.as 

package
{
    import mx.core.mx_internal;
    
    import spark.components.ComboBox;
    
    public class CaseSensComboBox extends ComboBox
    {
        public function CaseSensComboBox()
        {
            super();
        }
        
        [Bindable]private var _caseSens:Boolean
        public function get caseSens():Boolean
        {
            return _caseSens;
        }

        public function set caseSens(value:Boolean):void
        {
            _caseSens = value;
            if(value)
            {
                this.itemMatchingFunction = caseSensitiveFilterFunc ;
            }
        }
        
        protected function caseSensitiveFilterFunc(comboBox:ComboBox, inputText:String):Vector.<int>
        {
            var matchingItems:Vector.<int>;
            
            if (!dataProvider || dataProvider.length <= 0)
                return matchingItems;
            
            if (textInput.text != “”)
            {
                matchingItems = findMatchingItems(textInput.text);
            }
            else
            {
                // If the input string is empty, then don’t select anything
                this.mx_internal::changeHighlightedSelection(NO_SELECTION);
              }
            
            return matchingItems;
                
        }
        
        // Returns an array of possible values
        private function findMatchingItems(input:String):Vector.<int>
        {
            // For now, just select the first match
            var startIndex:int;
            var stopIndex:int;
            var retVal:int;  
            var retVector:Vector.<int> = new Vector.<int>;
            
            retVal = findStringLoop(input, 0, dataProvider.length);
            
            if (retVal != -1)
                retVector.push(retVal);
            return retVector;
        }
        
        private function findStringLoop(str:String, startIndex:int, stopIndex:int):Number
        {
            // Try to find the item based on the start and stop indices.
            for (startIndex; startIndex != stopIndex; startIndex++)
            {
                var itmStr:String = itemToLabel(dataProvider.getItemAt(startIndex));
                
                itmStr = itmStr.substring(0, str.length);
                if (str == itmStr)
                {
                    return startIndex;
                }
            }
            return -1;
        }

    }
}

 

Advertisements

, ,

  1. #1 by skringas on September 1, 2012 - 12:15 pm

    Sweet. Thanks for that! Just what I needed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: