0
votes

Basically what I want is to filter the values from a data grid. I have seen several examples which have done this successfully. Like these

http://dalvinder.serveblog.net/2011/06/25/flex-spark-datagrid-filtering/?

http://blog.bobklaas.com/?p=126?

The problem is all of these examples are using a predefined arraycollection as a dataprovider for their data grid while I'm using a callresponder result as a dataprovider. Currently, I'm using php services to retrieve data from the database and to my populate my datagrid so my code looks like this

    <mx:DataGrid x="34.3" y="99.65" id="dataGrid"   creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getAllStudentsResult2.lastResult}"  width="711" headerHeight="25" change="dataGrid_changeHandler(event)" styleName="table" height="367"
                     alternatingItemColors="[#e0e0e0, #cacaca]"
                     horizontalScrollPolicy="auto">

It seems that I can't filter the values from a callresponder result so I tried to transfer the callresponder results to another arraycollection using the resultevent handler like this

[Bindable]private var searchArray:ArrayCollection = new ArrayCollection();//variable declaration


protected function getAllStudents2Result_resultHandler(event:ResultEvent):void
        {
            searchArray = event.result as ArrayCollection;  
        }

and then i changed my datagrid provider to this

dataProvider="{searchArray}"

I then used this code from blog.bobklaas.com/?p=126 to filter my datagrid

            public function filterResults():void
        {

            var filterText:String = StringUtil.trim(keyword.text.toLowerCase()); //Trimmed text String to filter by
            var columnArray:Array = ['ID','FirstName','Lastname', 'Location', 'Level']; //Datagrid column names to filter on
            var gridDataProvider:ArrayCollection = searchArray; //Name of datagrid's dataprovider. In this case e.g. databases
            var dataGridName:String = 'dataGrid'; //Name of the datagrid you are filtering by

            //Do not edit code past this point
            var filteredData:Array = [];
            var added:Boolean=false;
            var i:int;
            var j:int;

            // Loop Through Grid
            for(i=0; i < gridDataProvider.length; i++){    
                added = false;
        //  Alert.show(gridDataProvider.getItemAt(i).toString());
                //Loop through grid column
                for(j=0; j<columnArray.length; j++){  


                    if(gridDataProvider[i][columnArray[j]]!=null){
                        //Grab datagrid cell contents, trim it, and convert to lowercase for comparison.
                        var filterString:String = gridDataProvider[i][columnArray[j]].toString().toLowerCase();

                        //Compare the datagrid string(filterString) to the user typed string(filterText).  
                        if(!added){      
                            //If the datagrid string matches the users string, put it into the array.

                            if(filterString.indexOf(filterText) != -1){

                                filteredData.push(gridDataProvider[i]);
                                added = true;
                            } 
                        }else{
                            //Do nothing, break out.
                            break;
                        }
                    }    
                }
            }

            //Set datagrid dataprovider
            if(filterText.length == 0){
                this[dataGridName].dataProvider = gridDataProvider; //Display the original unfiltered data
            }else{
                this[dataGridName].dataProvider = filteredData; //Pusht he filtered data into the datagrid
            }
        }

After I ran the application nothing happens. I still can't filter the values from the searchArray.

Can anyone suggest better solutions or am i doing something wrong???

1
Paste full sample application code... so that we can suggest what you have done wrong in the code... don't describe part by part... as it doesnot show full sample and mistake/logic while writing codeMahesh Parate
>After I ran the application nothing happens. Something happened. Do you use flashbuilder debugger? when do you call filterResult()?StephenNYC

1 Answers

0
votes

It seams is blank becase in: searchArray = event.result as ArrayCollection;

you're copy the pointer addres and not the data; if you delete search array, then event.result also will deleted.

I have the same problem, I couldnt find filter on call responder, here is my code

protected function getServiciosResult_resultHandler(event:ResultEvent):void
        {
            var i:int;
            var searchArray2:ArrayCollection;
            searchArray2= event.result as ArrayCollection;
            searchArray = new ArrayCollection();
            for(i=0;i<searchArray2.length;i++){
                searchArray.addItem(searchArray2[i]);
            }

        }

If you note, I'm copy the data from searchArray2 to searchArray. Thanks for your code, it helped me to do my code.