<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init()">
<mx:Script>
<![CDATA[
private var keys:Object = {};
/**
* Called by the Application container's creationComplete
* event handler. This method creates a new Array object
* which will be used as a data provider as well as a
* filtered view of that array which does not contain
* duplicated items.
*/
private function init():void {
/* Create a dummy data source with some semi-random
data. */
var arr:Array = [];
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:1, label:"one"});
arr.push({data:3, label:"three"});
arr.push({data:3, label:"three"});
/* Filter the original array and call the
removeDuplicates() function on each item
in the array. */
var filteredArr:Array = arr.filter(removedDuplicates);
arrColl.source = arr;
dedupedArrColl.source = filteredArr;
}
/**
* This method is used to filter an array so that no
* duplicate items are created. It works by first
* checking to see if a keys object already contains
* a key equal to the current value of the item.data
* value. If the key already exists, the current item
* will not be readded to the data provider. If the key
* does not already exist, add the key to the keys
* object and add this item to the data provider.
*/
private function removedDuplicates(item:Object, idx:uint, arr:Array):Boolean {
if (keys.hasOwnProperty(item.data)) {
/* If the keys Object already has this property,
return false and discard this item. */
return false;
} else {
/* Else the keys Object does *NOT* already have
this key, so add this item to the new data
provider. */
keys[item.data] = item;
return true;
}
}
]]>
</mx:Script>
<mx:ArrayCollection id="arrColl" />
<mx:ArrayCollection id="dedupedArrColl" />
<mx:HBox>
<mx:VBox>
<mx:Label text="Original ({arrColl.length} items):" />
<mx:List dataProvider="{arrColl}" />
</mx:VBox>
<mx:VBox>
<mx:Label text="Filtered ({dedupedArrColl.length} items):" />
<mx:List dataProvider="{dedupedArrColl}" />
</mx:VBox>
</mx:HBox>
</mx:Application>
Showing posts with label filter. Show all posts
Showing posts with label filter. Show all posts
Monday, August 6, 2007
Removing duplicate items from an array using the Array.filter() method
source : here
Saturday, August 4, 2007
Using the to ArrayCollection's filterFunction
source : here
and here
Problem Summary
Finding a way to to filter data in a DataGrid regardless of the data in question case.
and here
Problem Summary
Finding a way to to filter data in a DataGrid regardless of the data in question case.
Solution Summary
Use ArrayCollection's filterFunction and compare the data's string with a string you provide using RegEx to disregard the strings case (upper, lower or whatever)
Explanation
1. First create an array.
2. Pass that array to an ArrayCollection.
3. Pass ArrayCollection as DataProvider for a DataGrid
4. Create a textInput
5. Create a function that will filter data from the textInput text, return type Boolean
6. Now call the filterFunction method of the ArrayCollection class, pass the filterFunction to created
7. create an Event listener for knowing when to filter (eg. textinput, click)
8. create a reset button for clearing the filter.
9. That's essentially it. See example and code to see the full scope and that should explain it self
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init();" viewSourceURL="srcview/index.html">
<mx:Script>
<![CDATA[
import mx.collections.*;
private var collectionArray:Array;
[Bindable]
private var collectionData:ArrayCollection;
private var selectedName:String = "first";
import mx.controls.Alert;
import mx.events.ItemClickEvent;
private function init():void
{
collectionArray = [{first: 'Dave', last: 'Matthews'},
{first: 'Dave', last: 'Chappelle'},
{first: 'Amy', last: 'Grant'},
{first: 'Bilbo', last: 'Baggins'},
{first: 'Jessica', last: 'Tandy'},
{first: 'Jessica', last: 'Simpson'},
{first: 'Paris', last: 'Hilton'}];
collectionData = new ArrayCollection(collectionArray);
}
public function filter():void {
collectionData.filterFunction = filterFirst;
collectionData.refresh();
}
public function filterReset():void {
collectionData.filterFunction = null;
collectionData.refresh();
}
private function filterFirst(item:Object):Boolean
{
return item[selectedName].match(new RegExp(searchField.text, 'i'))
}
private function search():void
{
if(searchField.text !='')
{
filter()
}
else
{
filterReset()
}
}
private function handleCard(event:ItemClickEvent):void {
selectedName = event.currentTarget.selectedValue as String;
}
private function eraseText(event:MouseEvent):void
{
searchField.text ='';
}
]]>
</mx:Script>
<mx:DataGrid id="nameGrid" dataProvider="{collectionData}" height="210"/>
<mx:VBox y="{nameGrid.height + 10}">
<mx:TextInput tabEnabled="true" tabIndex="1" text="Search First Name" id="searchField" toolTip="Filter First Name" change="search()" rollOver="eraseText(event)" />
<mx:HBox>
<mx:RadioButtonGroup id="nameGroup" itemClick="handleCard(event)"/>
<mx:RadioButton tabEnabled="true" tabIndex="2" label="first" selected="true" groupName="nameGroup"/>
<mx:RadioButton tabEnabled="true" tabIndex="3" label="last" groupName="nameGroup" />
<mx:Button tabEnabled="true" tabIndex="4" click="searchField.text='';search()" label="Reset" />
</mx:HBox>
</mx:VBox>
</mx:Application>
Subscribe to:
Posts (Atom)