So I have in my RIA a mx:DataGrid connected to some web service from which it obtains data (Dataprovider). I have a simple text fild. I want to on text in text field change to see in my table only lines containing inputed text in some part of any word in any column. how to do such thing?
+1
A:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init();">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.events.TextOperationEvent;
[Bindable]
private var dataProvider:ArrayCollection;
private var filterString:String;
private function init():void
{
dataProvider = new ArrayCollection(
[
{ name: "Alan", surname: "Cooper" },
{ name: "James", surname: "Gosling" },
{ name: "Mike", surname: "Chambers" },
{ name: "Hare", surname: "Krishna" },
{ name: "Otto", surname: "Thunder" }
]);
dataProvider.filterFunction = filterFunction;
}
private function filterFunction(item:Object):Boolean
{
if (!filterString)
return true;
for (var p:String in item)
{
var string:String = item[p] as String;
if (!string)
continue;
if (string.toLocaleLowerCase().indexOf(filterString) >= 0)
return true;
}
return false;
}
private function input_changeHandler(event:TextOperationEvent):void
{
filterString = input.text.toLocaleLowerCase();
dataProvider.refresh();
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
</s:layout>
<s:TextInput id="input" change="input_changeHandler(event)"/>
<mx:DataGrid dataProvider="{dataProvider}"/>
</s:Application>
Maxim Kachurovskiy
2010-10-17 10:35:33