This example might help
http://tutorial7.flexcf.com/
Paul.
On Sep 19, 2009, at 7:57 PM, ew6014 wrote:
hi guys. ive been searching for a easy way to search within a
datagrid and found an answer from flex 3 cookbook from orielly. but
for some strange reason it does not work. when i click on search it
doesnt do anything. can someone help me please
here is the code for the application
------------------------------------
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="initApp()">
<mx:HTTPService id="srv" url="assets/homesforsale.xml"
resultFormat="object"
result="onResult(event)"/>
<mx:Form>
<mx:FormItem label="Search">
<mx:TextInput id="search_ti"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Search City" click="searchCity()"/>
</mx:FormItem>
</mx:Form>
<mx:DataGrid id="grid" width="300" height="150" editable="true"
dataProvider="{homesForSale}">
<mx:columns>
<mx:DataGridColumn headerText="Total No." dataField="total"/>
<mx:DataGridColumn headerText="City" dataField="city"/>
<mx:DataGridColumn headerText="State" dataField="state"/>
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.collections.IViewCursor;
import mx.events.FlexEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var homesForSale:ArrayCollection;
private var cursor:IViewCursor;
private function initApp():void {
this.srv.send();
}
private function onResult(evt:ResultEvent):void {
var sort:Sort = new Sort();
sort.fields = [ new SortField("city",true) ];
this.homesForSale = evt.result.data.region;
this.homesForSale.sort = sort;
this.homesForSale.refresh();
this.cursor = this.homesForSale.createCursor();
}
private function searchCity():void {
if(search_ti.text != "") {
if(this.cursor.findFirst({city:search_ti.text})){
var idx:int = this.homesForSale.getItemIndex(this.cursor.current);
this.grid.scrollToIndex(idx);
this.grid.selectedItem = this.cursor.current;
}
}
}
]]>
</mx:Script>
</mx:Application>
here is the code for the xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<data>
<region>
<city>Tampa</city>
<state>FL</state>
<total>1002</total>
<range>
<range1>70</range1>
<range2>20</range2>
<range3>10</range3>
</range>
</region>
<region>
<city>Boston</city>
<state>MA</state>
<total>2705</total>
<range>
<range1>30</range1>
<range2>60</range2>
<range3>10</range3>
</range>
</region>