0

基本上我想要的是从数据网格中过滤值。我见过几个成功做到这一点的例子。像这些

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

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

问题是所有这些示例都使用预定义的 arraycollection 作为其数据网格的数据提供者,而我使用 callresponder 结果作为数据提供者。目前,我正在使用 php 服务从数据库中检索数据并填充我的数据网格,因此我的代码如下所示

    <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">

似乎我无法从 callresponder 结果中过滤值,所以我尝试使用这样的 resultevent 处理程序将 callresponder 结果传输到另一个 arraycollection

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


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

然后我将我的数据网格提供程序更改为此

dataProvider="{searchArray}"

然后我使用 blog.bobklaas.com/?p=126 中的这段代码来过滤我的数据网格

            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
            }
        }

在我运行应用程序后,没有任何反应。我仍然无法过滤来自 searchArray 的值。

任何人都可以提出更好的解决方案还是我做错了什么???

4

1 回答 1

0

它接缝是空白的,因为: searchArray = event.result as ArrayCollection;

您要复制指针地址而不是数据;如果你删除搜索数组,那么 event.result 也会被删除。

我有同样的问题,我在呼叫响应者上找不到过滤器,这是我的代码

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]);
            }

        }

如果您注意到,我将数据从 searchArray2 复制到 searchArray。感谢您的代码,它帮助我完成了我的代码。

于 2013-04-23T06:42:39.493 回答