How to search a string within a flex datagrid dataProvider 4?
I tried using filterFunction not get any results, check out this example:
[Bindable]
public var SearchLoadlistOneDP:ArrayCollection;
public function SearchList():void {
SearchLoadlistOneDP.filterFunction = filter;
SearchLoadlistOneDP.refresh()
}
public function filter(item:Object):Boolean
{
var beginsWithString:String = SearchLoadlistOneInput.text;
return String(item["email"]).indexOf(beginsWithString) == 0;
}
And I used the datagrid like this:
<mx:DataGrid x="10" dataProvider="{SearchLoadlistOneDP}" y="49" width="891" height="408" id="listam开发者_Go百科ail" creationComplete="LoadlistOne(0)">
<mx:columns>
<mx:DataGridColumn headerText="id" dataField="id" width="80"/>
<mx:DataGridColumn headerText="E-mail" dataField="email"/>
<mx:DataGridColumn headerText="Nome" dataField="nome"/>
</mx:columns>
</mx:DataGrid>
And text input like this:
<s:TextInput x="62.6" y="9.75" width="408" id="SearchLoadlistOneInput" keyUp="SearchList()" />
But unfortunately I can not any results, and the datagrid already has data from the database already has data listed on it.
Can anyone help?, Thank you all now
Here is what your filter function should look like. I have been using this method for awhile and have had no problems with it. This works with Flex 3 & 4.
private function filter(item:Object):Boolean{
//this is going to return true if we don't have anything to search by, which will return
//all items the the datagrid
if(SearchLoadlistOneInput.text == ""){
return true;
}
//now search the string and see if we have a match//
if(item.email.toLowerCase().search(SearchLoadlistOneInput.text.toLowerCase()) != -1){
return true;
}
return false;
}
Flex SDK 4.0.0, This works for me:
<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" minWidth="955" minHeight="600" applicationComplete="_init();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var SearchLoadlistOneDP:ArrayCollection;
private function _init() :void
{
SearchLoadlistOneDP = new ArrayCollection();
SearchLoadlistOneDP.addItem({id: 1, email: 'fake@null.com', name:'Bill'});
SearchLoadlistOneDP.addItem({id: 2, email: 'test@null.com', name:'Todd'});
SearchLoadlistOneDP.addItem({id: 3, email: 'null@null.com', name:'Beth'});
SearchLoadlistOneDP.addItem({id: 4, email: 'void@null.com', name:'Tiffany'});
}
public function SearchList():void {
SearchLoadlistOneDP.filterFunction = filter;
SearchLoadlistOneDP.refresh()
}
public function filter(item:Object):Boolean
{
var name:String = String(item["email"]);
var beginsWithString:String = SearchLoadlistOneInput.text;
return name.indexOf(beginsWithString) == 0;
}
]]>
</fx:Script>
<mx:DataGrid x="10" dataProvider="{SearchLoadlistOneDP}" y="49" width="891" height="408" id="listamail">
<mx:columns>
<mx:DataGridColumn headerText="id" dataField="id" width="80"/>
<mx:DataGridColumn headerText="E-mail" dataField="email"/>
<mx:DataGridColumn headerText="Name" dataField="name"/>
</mx:columns>
</mx:DataGrid>
<s:TextInput x="62.6" y="9.75" width="408" id="SearchLoadlistOneInput" keyUp="SearchList()" />
</s:Application>
精彩评论