Search code examples
xmlactionscript-3apache-flexflex4

Flex 4 - Filter is not working


I am doing filtering in spark DataGrid. But I can't filter the data. And I am using dataprovider as a XML file. My code is here

<s:Application 
xmlns:fx="http://ns.adobe.com/mxml/2009"    
xmlns:mx="library://ns.adobe.com/flex/mx"     
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private function xmlListCollectionFilterFun(item : Object) : Boolean
        {
            var token : String = employeeName.text.toLowerCase();
            var eName : String = employeeXMLList.child("Name");
            var col : XMLList=tempXML.Employee.Name;
            if(eName.toLowerCase().indexOf(token)!= -1)
            {
                return true;
            }
            return false;
        }


        protected function employeeName_changeHandler():void
        {
            if( employeeName.text.length == 0)
            {
                employeeXMLList.filterFunction = null;
            }
            else
            {
                employeeXMLList.filterFunction = xmlListCollectionFilterFun;
            }
            employeeXMLList.refresh();

        }

    ]]>
</fx:Script>


<fx:Declarations>

    <fx:XML id="tempXML"
            source="skins/TextXmlFile.xml" />

    <s:XMLListCollection id="employeeXMLList"
                         source="{tempXML.Employee}" filterFunction="xmlListCollectionFilterFun" />
</fx:Declarations>

<mx:VBox width="80%">

    <s:TextInput id="employeeName" change="employeeName_changeHandler()"/>
    <s:DataGrid id="dataGrid" dataProvider="{employeeXMLList}" width="100%" >
        <s:columns>
            <s:ArrayCollection>
                <s:GridColumn id="nameCol" dataField="Name" headerText="Name:" />
                <s:GridColumn id="idCol" dataField="Id" headerText="ID:"/>
                <s:GridColumn id="mobileCol" dataField="Mobile" headerText="Mobile:"/>
                <s:GridColumn id="alterCol" dataField="AlterMobile" headerText="Alternative Number"/>
            </s:ArrayCollection>
        </s:columns>
    </s:DataGrid>
</mx:VBox>
</s:Application>

And my XML file is :

<?xml version="1.0" encoding="UTF-8"?>
<CompanyEmployees version="1">   

<Employee>
    <Name>John</Name>
    <Id>Em234</Id>
    <Mobile>09999999999</Mobile>
    <AlterMobile>yes</AlterMobile>>
</Employee>   

<Employee>
    <Name>Ram</Name>
    <Id>Em432</Id>
    <Mobile>8967452354</Mobile>
    <AlterMobile>yes</AlterMobile>
</Employee>   

<Employee>
    <Name>Raj</Name>
    <Id>Em098</Id>
    <Mobile>02343235478</Mobile>
    <AlterMobile>no</AlterMobile>
</Employee>   

It display's no error. But I couldn't find where and what is my mistake? If anybody find it, Please intimate me.

Thank you


Solution

  • Your filter function takes in consideration whole data instead of given single item. Thus it might always return all items, because searched text is composed from all names. Try following code:

    private function xmlListCollectionFilterFun(item:Object):Boolean {
        var token:String = employeeName.text.toLowerCase();
        var eName:String = item.Name;
        if(eName.toLowerCase().indexOf(token)!= -1) {
            return true;
        }
        return false;
    }