Domanda

I have an XML file Which contains Employee details . And I have to use filter on it. Here, I want to filter the experience using two NumericStepper. If I select 1 and 4 for first and second NumericStepper, DataGrid Will display the employee list which are the employee's has experienced between 1 to 4.

Here my code:

    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        import mx.utils.ObjectUtil;

        private function xmlListCollectionFilterFun(item : Object) : Boolean
        {
            if(employeeName.text.length !=0)
            {
                if((item.Name).toLowerCase().indexOf(employeeName.text.toLowerCase())!= -1)
                {
                    return true;
                }
            }
            if(employeeID.text.length != 0)
            {
                if((item.Id).toLowerCase().indexOf(employeeID.text.toLowerCase()) != -1)
                {
                    return true;
                } 
            }

            if(endYear.value != 0)
            {
                if((startYear.value)<=(item.Experience)<=(endYear.value))
                {
                    return true;
                }
            } 

            return false;
        }

        protected function employeeText_changeHandler():void
        {
            if( employeeName.text.length == 0 && endYear.value == 0 &&
                employeeID.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>

<s:layout>
    <s:VerticalLayout verticalAlign="top" horizontalAlign="center" paddingTop="30"/>
</s:layout>
<mx:VBox width="100%">
    <s:HGroup width="100%">
        <s:TextInput id="employeeName" change="employeeText_changeHandler()" prompt="Employee Name"/>
        <s:TextInput id="employeeID" prompt="Employee ID" change="employeeText_changeHandler()"/>
        <s:NumericStepper id="startYear" minimum="0" maximum="50" snapInterval="1" />
        <s:NumericStepper id="endYear" minimum="0" maximum="50" snapInterval="1" change="employeeText_changeHandler()"/>
    </s:HGroup>
    <s:DataGrid id="dataGrid" dataProvider="{employeeXMLList}" width="100%" height="100%">
        <s:columns>
            <s:ArrayCollection>
                <s:GridColumn id="nameCol" dataField="Name" headerText="Name:"/>
                <s:GridColumn id="idCol" dataField="Id" headerText="ID:"/>
                <s:GridColumn id="experienceCol" dataField="Experience" headerText="Experience:"/>
            </s:ArrayCollection>
        </s:columns>
    </s:DataGrid>

This is code is no effect for NumericStepper. If anyone can find my mistake?

È stato utile?

Soluzione

Try always to split your code in small functions that do a single thing, I would do it like this:

 getUserExperience(user:Object):Number

implement this function first do some test and after you get it o return the correct experience then you can go to the next step to make the filter function.

Write some code to compute the min and max experience for the filter

var min:Number=startYear.value;
var max:Number=endYear.value;//if needed adjust this to seome default values

now the filtering should be easy because you just have numbers and no UI components and Objects so the filter will need to check a simple thing

var userExp:Number=getUserExperience(user);
var response:Boolean=mio<=userExp&&userExp<=max;

return response;

I am sure after you refactor your code to be simple and clear it will be easy to fix any bugs

Altri suggerimenti

Is it a typo in your code or a problem with stack overflow that doesn't display some chars (the OR pipes I assume) ?

employeeName.text.length == 0 endYear.value == 0 &&
            employeeID.text.length == 0

update: even with pipes, this test does not make sense.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top