Submitted by Abhisek Paul on Sat, 01/28/2012 - 18:20
Finally i decided to write my blogs...So lets see what i can start with... Well in my recent Flex project, i wanted to search an instance of an item from an arraycollection(or for that matter from any collection) whose one of its property is equal to my expected property . Well there are many ways to achieve this, but i wanted sometihng generic which i can use for any collection. I have written two functions, one returns the idex of the item and other returns the item itself.
So here is my utility class:
MyUtilClass
{
private static function findFunction( propertyName:String, propertyValue:* ):Function
{
return function( element : *, index : int, array : Array ) : Boolean
{
return element[propertyName] == propertyValue;
}
}
public static function findInCollection(collection:ArrayCollection, propertyName:String, propertyValue:*):Object
{
var filterfunction:Function=findFunction(propertyName,propertyValue);
var matches : Array = collection.source.filter( filterfunction );
return ( matches.length > 0 ? matches[0] : null );
}
public static function findIndexInCollection(collection:ArrayCollection, propertyName:String, propertyValue:*):int
{
var filterfunction:Function=findFunction(propertyName,propertyValue);
var matches : Array = collection.source.filter( filterfunction );
return ( matches.length > 0 ? collection.getItemIndex(matches[0]): -1 );
}
}
Usage:
var myitem:Object=MyUtilClass.findInCollection(employeeCollection, "name","Abhisek");
myitem.someproperty="something";
- Abhisek Paul's blog
- Log in to post comments
6 comments
qgnAkbaRvEumhX
Have you tried using eanlbe/disable auto update then refreshing at the right time?You can disable the auto update first so nothing shows in your list then accomplish your filter and eanlbe auto update then force a refresh on your collection to update the list.
ysQxxSIHsFHCUjWJrNr
WirawannagaThis funitcon so great the problem is i still can do it using XML came from HTTP. Can u teach how to work with external XML?
check this one http:/
check this one http://fridaymushroom.com/fm/xml-to-arraycollection
Search/Filter on Multiple fields
Hi Abhisek,
This is very useful but would be the greatest if it could also do these:
1. search on multiple fields rather then one. i.e. state=TX and Zipcode=75093, now I concatenate state and zip in one field store in array collection and search it that way. You would add 2 more functions findMultiInCollection and findMultiIndexInCollection.
2. If you do this for filtering rows in arraycollection. Pass the source collection and field/value pair/s to filter the array collection by.
3. Same thing with the sort.
This would take care of everything anybody would want to do with ArrayCollections. You can keep it simple and assume these array collections are flat tables and not deep nested structures.
this is a good suggestion, i
this is a good suggestion, i would post the updated code sometime later. In all my project, i never required multifield search, however having that capability would be good.
For your immediate need, u can always extend the functionalist, with modifying the arguments with chaining of properties, similar to BindUtils.bindproperty.
Search an item based on a property in arraycollection in Flex
Hey there, You have done an incredible job. I'll certainly digg it and personally recommend to my friends. I'm confident they will be benefited from this web site.