File: /home/mmickelson/theflexguy.com/files/SearchInput.as
package com.theflexguy
{
import flash.events.Event;
import mx.collections.ICollectionView;
import mx.controls.TextInput;
public class SearchInput extends TextInput
{
// dataProviderToFilter must implement ICollectionView so a
// FilterFunction can be applied to it.
public var dataProviderToFilter:ICollectionView;
// propertiesToSearch is an Array of property names as Strings
public var propertiesToSearch:Array;
private var _searchWords:Array;
// Splits the search text up using spaces to get the "words",
// then sets the filterFunction
private function searchTxt_changeHandler(event:Event):void
{
if (text != "")
{
// split up the input into individual "words", made upperCase so
// it's case insensitive
_searchWords = text.toUpperCase().split(" ");
// check to see if the last item is a 0 length String, this
// would be the case if the last character typed was a space
if (_searchWords[_searchWords.length - 1] == "")
{
// if it is "", remove it
_searchWords.pop();
}
// set the filterFunction
dataProviderToFilter.filterFunction = filterForSearch;
}
else
{
// the user removed the text so remove the filterFunction
dataProviderToFilter.filterFunction = null;
}
// refresh the dataProvider
dataProviderToFilter.refresh();
}
// Uses the searchWords array to search the summary and components
// fields of each row for each item in the array.
private function filterForSearch(item:Object):Boolean
{
var match:Boolean = false
for each(var s:String in _searchWords)
{
for each(var p:String in propertiesToSearch)
{
// check each property for the word using toUpperCase so
// it's case insensitive
if (item[p].toString().toUpperCase().match(s))
{
match = true;
break;
}
}
}
return match;
}
public function SearchInput()
{
super();
addEventListener(Event.CHANGE, searchTxt_changeHandler);
}
}
}