xmlactionscript-3apache-flexflex4filterfunction

Using filterFunction to do filtering based on more than 1 parameter


I have defined an XMLList which gets its data from the following XML file:

    <modules>
        <module hab_id="1" module_id="1" default="true" access="true" />
        <module hab_id="1" module_id="2" default="true" access="true" />
        <module hab_id="1" module_id="2" default="true" access="false" />
        <module hab_id="1" module_id="2" default="false" access="true" />
        <module hab_id="2" module_id="3" default="true" access="true" />
        <module hab_id="2" module_id="3" default="false" access="true" />
    </modules>

Now in my function, lets upon clicking a button I want to filter by both hab_id and module_id at the same time and populate a datagrid.

I tried this in the function:

    public function click_Handler(event:MouseEvent):void{
        myXMLList.filterFunction = myFilter;
        myXMLList.refresh();
        myDatagrid.dataProvider = myXMLList;
    }

    private function myFilter(xml:XML):Boolean{
                    return Number(xml.@hab_id) == 1;
                    return Number(xml.@module_id) == 2;
                }

But the filter is only working for the hab_id it seems. It is not filtering by both the hab_id and module_id.

Any help on this?


Solution

  • Try this:

    private function myFilter(xml:XML):Boolean{
       return Number(xml.@hab_id) == 1 && Number(xml.@module_id) == 2;
    }
    

    Your first return would return from the function, never allowing the second return to execute. Combining the equality checks into a single return value should give you what you want.