Tuesday 17 July 2012

How To Use Filter Function In Flex

How To Use Filter Function In Flex

We Can use Filter Function Filer Data In Array Collection.

Go through The Code Following Example Filter Data For
Name and Salary Fields


<?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" minWidth="955" minHeight="600" xmlns:local="*" viewSourceURL="srcview/index.html" initialize="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.as3xls.xls.Cell;
import com.as3xls.xls.ExcelFile;
import com.as3xls.xls.Sheet;

import mx.collections.ArrayCollection;
import mx.controls.Alert;

private var fileReference:FileReference;
private var xls:Class;
private var sheet:Sheet;

[Bindable]
public var ItemDGDataProvider:ArrayCollection = new
ArrayCollection([
{name:"Ajay",salary:"25000",jDate:"5-Jun-2010",desg:"Soft Engineer"},
{name:"Mayur",salary:"28000",jDate:"13-Sep-2010",desg:"Soft Tester"},
{name:"Ashish",salary:"34500",jDate:"5-Jan-2011",desg:"Project manager"},
{name:"Deepak",salary:"28000",jDate:"17-Feb-2011",desg:"Business Analyst"},
{name:"Ravi",salary:"22400",jDate:"7-Mar-2010",desg:"Graphics Designer"},
{name:"Summet",salary:"18000",jDate:"5-Jun-2010",desg:"Trainee"},
{name:"Sanket",salary:"35000",jDate:"18-July-2011",desg:"QA Lead"},
{name:"Arun",salary:"15000",jDate:"4-Apr-2012",desg:"Graphics Designer"},
{name:"Dinesh",salary:"25000",jDate:"5-Jun-2010",desg:"Soft Engineer"},
{name:"Mrunal",salary:"28000",jDate:"13-Sep-2010",desg:"Soft Tester"},
{name:"Sanjay",salary:"34500",jDate:"5-Jan-2011",desg:"Project manager"},
{name:"Abhishek",salary:"28000",jDate:"17-Feb-2011",desg:"Business Analyst"},
{name:"Shiv",salary:"22400",jDate:"7-Mar-2010",desg:"Graphics Designer"},
{name:"Raja",salary:"18000",jDate:"5-Jun-2010",desg:"Trainee"},
{name:"Pratic",salary:"35000",jDate:"18-July-2011",desg:"QA Lead"},
{name:"Vishal",salary:"15000",jDate:"4-Apr-2012",desg:"Graphics Designer"}

]);
public function init():void
{
ItemDGDataProvider.filterFunction = filterForTestData ;
}
private function filterForTestData(item:Object):Boolean{
var isMatch:Boolean = false
if(item.name.toLowerCase().search(txtFilter.text.toLowerCase()) != -1){
isMatch = true
}
if(item.salary.search(txtFilter.text.toLowerCase()) != -1){
isMatch = true
}
return isMatch;
}

/* the filter funcion is called when refresh() method is invoked on
the collection */
private function applyFilter():void{
ItemDGDataProvider.filterFunction = filterForTestData
ItemDGDataProvider.refresh();
}


]]>
</fx:Script>
<mx:TextInput id="txtFilter" x="4" y="2" width="202" change="applyFilter()"/>
<mx:DataGrid id="rebateByItemDG"
includeInLayout="true"
visible="true"
dataProvider="{ItemDGDataProvider}" width="100%"
editable="true" x="1" y="28"/>
</s:Application>



No comments:

Post a Comment