“Filter results in DataGrid” Flex Tutorial
Tuesday, April 29th, 2008- Custom header in DataGrid
- Custom header in DataGrid - part 2
- “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.
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.
tagged under:






2 Comments
Yulia
• Visit Site
April 30th, 2008
Hi,
I’m interested in buying links from your blog
Please contact me by email
Thanks
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