基本上我想要的是过滤数据网格中的值。我已经看到几个成功完成这个的例子。像这些
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中的值。
任何人都可以提出更好的解决方案,或者我做错了什么?
答案 0 :(得分: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。 感谢您的代码,它帮助我完成了我的代码。