在上文"Flex AdvancedDataGrid表格过滤"中介绍了flat data和HierarchicalData的过滤处理,并且提到所谓表格过滤实质上就是表格的数据源过滤。本文继续假设表格的最终数据源就是Arraycollection,则表格过滤通用化处理,就是arraycollection的filterFunction通用化处理。
思路如下:
1)构造过滤条件列表;
2)过滤函数实现,根据过滤条件列表逐个进行验证,最终返回true或者false验证结果;
实现如下:
1)构造过滤条件列表
条件列表本身是一个array Collection,假设为conditions,其放置的对象为条件过滤对象,主要有4个元素:
column:要过滤的列名;
operator:操作符,有等于(=)、小于()、不等于(!=)、小于等于(=)、like(.)、not like(!.);
comparevalue:比较值
logical:逻辑值,and、or;
2)filterFunction实现
为了增加代码松耦合,将过滤处理的核心代码写成了一个类DataGridFilter,代码如下: publicclassDataGridFilter { privatestaticconst LOGICAL_AND:String = "AND"; privatestaticconst LOGICAL_OR:String = "OR"; // privatestaticconst OPERATOR_EQ:String = "="; privatestaticconst OPERATOR_LT:String = ""; privatestaticconst OPERATOR_LE:String = "="; privatestaticconst OPERATOR_NE:String = "!="; privatestaticconst OPERATOR_LIKE:String = "."; privatestaticconst OPERATOR_NOTLIKE:String = "!."; // publicfunctionDataGridFilter() { } publicfunction isMatched(item:Object, conditions:ArrayCollection):Boolean{ var ismatched:Boolean = true; for(var i:int=0; ivalue2; } } privatefunction isLE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1=value2; } } privatefunction isNE(value1:Object, value2:Object):Boolean{ returntrue; } privatefunction isLIKE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1.indexOf(value2)>-1; } } privatefunction isNOTLIKE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1.indexOf(value2)==-1; } } publicfunction isMatchedObject(item:Object, condition:Object):Boolean{ var value1:Object = item[condition["column"]]; var value2:Object = condition["comparevalue"]; switch(condition["operator"]){ case OPERATOR_EQ: return isEQ(value1,value2); break; case OPERATOR_LT: return isLT(value1,value2); break; case OPERATOR_GT: return isGT(value1,value2); break; case OPERATOR_LE: return isLE(value1,value2); break; case OPERATOR_GE: return isGE(value1,value2); break; case OPERATOR_NE: return isNE(value1,value2); break; case OPERATOR_LIKE: return isLIKE(value1,value2); break; case OPERATOR_NOTLIKE: return isNOTLIKE(value1,value2); break; } returntrue; } } 假设arraycollection.filterFunciton为filterFunc,则filterFunc实现如下:
privatefunctionfilterFunc(item:Object):Boolean{
var filter:DataGridFilter = new DataGridFilter();
// conditionList为过滤条件列表
return filter.isMatched(item,conditionList);
}
3)只要设置arraycollection的filterFunction为filterFunc,并且带上过滤条件列表即可实现表格的通用过滤处理,由于flat data和HierarchicalData数据源的构造方式不同,因此还需要做如下处理: if(conditionList.length>0){// conditionList过滤条件列表
if(_datagrid.dataProvider.source is HierarchicalData){
datagrid.dataProvider.source.source.filterFunction = filterFunc;
datagrid.dataProvider.source.source.refresh();
datagrid.dataProvider.source.refresh();
}else{
datagrid.dataProvider.filterFunction = filterFunc;
datagrid.dataProvider.refresh();
}
}else{
if(_datagrid.dataProvider.source is HierarchicalData){
datagrid.dataProvider.source.source.filterFunction = null;
datagrid.dataProvider.source.source.refresh();
datagrid.dataProvider.source.refresh();
}else{
datagrid.dataProvider.filterFunction = null;
datagrid.dataProvider.refresh();
}
}
思路如下:
1)构造过滤条件列表;
2)过滤函数实现,根据过滤条件列表逐个进行验证,最终返回true或者false验证结果;
实现如下:
1)构造过滤条件列表
条件列表本身是一个array Collection,假设为conditions,其放置的对象为条件过滤对象,主要有4个元素:
column:要过滤的列名;
operator:操作符,有等于(=)、小于()、不等于(!=)、小于等于(=)、like(.)、not like(!.);
comparevalue:比较值
logical:逻辑值,and、or;
2)filterFunction实现
为了增加代码松耦合,将过滤处理的核心代码写成了一个类DataGridFilter,代码如下: publicclassDataGridFilter { privatestaticconst LOGICAL_AND:String = "AND"; privatestaticconst LOGICAL_OR:String = "OR"; // privatestaticconst OPERATOR_EQ:String = "="; privatestaticconst OPERATOR_LT:String = ""; privatestaticconst OPERATOR_LE:String = "="; privatestaticconst OPERATOR_NE:String = "!="; privatestaticconst OPERATOR_LIKE:String = "."; privatestaticconst OPERATOR_NOTLIKE:String = "!."; // publicfunctionDataGridFilter() { } publicfunction isMatched(item:Object, conditions:ArrayCollection):Boolean{ var ismatched:Boolean = true; for(var i:int=0; ivalue2; } } privatefunction isLE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1=value2; } } privatefunction isNE(value1:Object, value2:Object):Boolean{ returntrue; } privatefunction isLIKE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1.indexOf(value2)>-1; } } privatefunction isNOTLIKE(value1:Object, value2:Object):Boolean{ if(value1==null){ returnfalse; }else{ return value1.indexOf(value2)==-1; } } publicfunction isMatchedObject(item:Object, condition:Object):Boolean{ var value1:Object = item[condition["column"]]; var value2:Object = condition["comparevalue"]; switch(condition["operator"]){ case OPERATOR_EQ: return isEQ(value1,value2); break; case OPERATOR_LT: return isLT(value1,value2); break; case OPERATOR_GT: return isGT(value1,value2); break; case OPERATOR_LE: return isLE(value1,value2); break; case OPERATOR_GE: return isGE(value1,value2); break; case OPERATOR_NE: return isNE(value1,value2); break; case OPERATOR_LIKE: return isLIKE(value1,value2); break; case OPERATOR_NOTLIKE: return isNOTLIKE(value1,value2); break; } returntrue; } } 假设arraycollection.filterFunciton为filterFunc,则filterFunc实现如下:
privatefunctionfilterFunc(item:Object):Boolean{
var filter:DataGridFilter = new DataGridFilter();
// conditionList为过滤条件列表
return filter.isMatched(item,conditionList);
}
3)只要设置arraycollection的filterFunction为filterFunc,并且带上过滤条件列表即可实现表格的通用过滤处理,由于flat data和HierarchicalData数据源的构造方式不同,因此还需要做如下处理: if(conditionList.length>0){// conditionList过滤条件列表
if(_datagrid.dataProvider.source is HierarchicalData){
datagrid.dataProvider.source.source.filterFunction = filterFunc;
datagrid.dataProvider.source.source.refresh();
datagrid.dataProvider.source.refresh();
}else{
datagrid.dataProvider.filterFunction = filterFunc;
datagrid.dataProvider.refresh();
}
}else{
if(_datagrid.dataProvider.source is HierarchicalData){
datagrid.dataProvider.source.source.filterFunction = null;
datagrid.dataProvider.source.source.refresh();
datagrid.dataProvider.source.refresh();
}else{
datagrid.dataProvider.filterFunction = null;
datagrid.dataProvider.refresh();
}
}