“Filter results in DataGrid” Flex Tutorial

Tuesday, April 29th, 2008
  1. Custom header in DataGrid
  2. Custom header in DataGrid - part 2
  3. “Filter results in DataGrid” Flex Tutorial

This is new post in my DataGrid Series Flex Tutorials. This tutorial shows you how to filter rows for searched keyword. It’s quite easy, since dataProvider for DataGrid is ArrayCollection, and ArrayCollection has variable filterFunction. It simple expect function which will filter correct rows for you. So basically, you have Datagrid and dataProvider is defined like this

[as]
[Bindable] public var dpRows:ArrayCollection;
[/as]

then you can create filter function in this simple way:

[as]
public function filterResults():void
{
dpRows.filterFunction = _sortRows;
dpRows.refresh();
}
[/as]

and finally function, which filters results. It returns true for row, which will be visible, and false for row which will be hiden.

[as]
private function _sortRows(item:Object):Boolean
{
var col:String = cmbCol.selectedItem.data as String;
var key:String = keyword.text;

key = key.toLowerCase();

if (key != “”)
{
if (col != “any”)
{
var value:String = item[col];
value = value.toLowerCase();

if (value.indexOf(key) >= 0)
{
return true;
}
} else {
for (var o:String in item)
{
value = item[o];
value = value.toLowerCase();
if (value.indexOf(key) >= 0)
{
return true;
}
}
}
} else {
return true;
}

return false;
}
[/as]

It’s quite simple tutorial. Here you can see source code for example which follows.

ArrayCollection filterFunction example screenshot

In next tutorial for DataGrid I will show you how you can add Button into cell. We will use custom itemRenderer for DataGridColumn.

If you have any questions, or suggestions for tutorial, please let me know…

Enjoy.

AddThis Feed Button

This is some text prior to the author information. You can change this text from the admin section of WP-Gravatar  I'm living in Bratislava, Slovakia. I'm Senior Flex, AIR Developer for my own company Flexets. Read more from this author


tagged under:

ABOUT THIS AUTHOR

I'm living in Bratislava, Slovakia. I'm Senior Flex, AIR Developer for my own company Flexets.

Get a Trackback link

2 Comments


  1. Yulia
    Visit Site
    April 30th, 2008

    Hi,
    I’m interested in buying links from your blog
    Please contact me by email
    Thanks


  2. Tink
    Visit Site
    May 2nd, 2008

    You might find the following useful too

    http://www.tink.ws/blog/filtereddatagrid/

    I implemented it this way as I was using the same ArrayCollection in more than one place, and I needed each to be filtered in a different way, without affecting the other (i.e. not affecting the original data).

Live Preview

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

This site is using OpenAvatar based on
Images is enhanced with WordPress Lightbox JS by Zeo
Clicky Web Analytics