简体   繁体   English

在Flex中使用comboBox过滤dataGrid

[英]Filter a dataGrid with a comboBox in flex

How can I filter a datagrid based on the value in the combo box? 如何根据组合框中的值过滤数据网格? Can anyone show me some good examples on it? 有人可以给我展示一些很好的例子吗?

In my application, I already filter a datagrid based on the text entered by the user. 在我的应用程序中,我已经根据用户输入的文本过滤了一个数据网格。 I check if the entered string matches the column entry of the datagrid and if a match is found,the filterFunction on the dataprovider is called. 我检查输入的字符串是否与datagrid的列条目匹配,如果找到匹配项,则调用dataprovider上的filterFunction。 All this I did with the help of a tutorial only, as I'm learning flex as I do my project. 我仅在教程的帮助下完成了所有这些工作,因为我在学习项目的同时就学习了flex。

This is that code: 这是该代码:

<mx:FormItem direction="horizontal">
    <mx:ComboBox id="searchCriteria1" dataProvider="{criteriaDP1}" change="searchFunction()"/>
    <mx:TextInput id="search" change="searchFunction()"/> 
    <mx:Button label="Clear Search" click="clear()" /> 
</mx:FormItem> 

private function searchFunction():void{
defectList.filterFunction = filterItems;
defectList.refresh();
}

private function filterItems(item:Object):Boolean
{
var isMatch:Boolean = false         

     if(searchCriteria1.selectedItem.label == "Defect Id")
{ 
    if(item.defId.toString().search(search.text.toString()) != -1)
    {
        isMatch = true
    } 
}
else if(searchCriteria1.selectedItem.label == "Review Id")
{
    if(item.revId.toString().search(search.text.toString()) != -1)
    {
        isMatch = true
    } 
}
     return isMatch;  

}

Here defectList is the dataprovider to the Data Grid, revId,defId are the columns of the data grid . 这里的defendList是数据网格的数据提供者,revId,defId是数据网格的列。

Now how do I filter, with combo boxes. 现在,如何使用组合框进行过滤。 I have a combo box called "priority" with values "high","medium","low","all". 我有一个名为“优先级”的组合框,其值分别为“高”,“中”,“低”,“全部”。 If I select all,no filtering is done. 如果我全选,则不执行任何过滤。 If I select "high" only those fields with priprity column value as "high" should appear. 如果我选择“高”,则仅应显示具有优先级列值为“高”的那些字段。

EDIT I even tried like this: 编辑我甚至尝试过这样:

 if(searchCriteria2.selectedItem.label=="Priority")
 {
      if (item.revType.toLowerCase().search(searchCriteria.selectedLabel.toLowerCase()) != -1)
 {
              Alert("yes");
    isMatch=true
 }
 }

searchCriteria is the comboBox,where I have the items, "ALL","HIGH"... I have two rows with value "high" and I get the Alert "yes" for two times only.. But in the data grid all the four rows are displayed. searchCriteria是comboBox,其中包含项目“ ALL”,“ HIGH” ...我有两行,其值均为“ high”,并且两次仅收到警报“ yes”。。但是在数据网格中,所有显示四行。

You appear to be searching on the revType column, rather than the Priority column. 您似乎在搜索revType列,而不是Priority列。

It may be worth finding a way to reduce the amount of duplicated code in your app to help avoid bugs like this. 可能值得寻找一种方法来减少应用程序中重复代码的数量,以帮助避免此类错误。 For example, your list of fields could look like this: 例如,您的字段列表可能如下所示:

[Bindable]
var criteriaDP1:ArrayCollection = new ArrayCollection([{label:"Review ID", value:"RevID"},
                                                       {label:"Defect Id", value:"DefID"}]); 

Setting it up like that would let you use the value field as an index on your dataProvider, like this: 像这样进行设置将使您可以将value字段用作dataProvider上的索引,如下所示:

public function search_clickHandler():void
{
    defectList.filterFunction = function(item:Object):Boolean
    {
        var gridValue:String = item[searchCriteria.selectedItem.value].toString().toLowerCase();
        var searchValue:String = search.text.toLowerCase(); 
        if(gridValue.search(searchValue) != -1)
        {
            return true;                        
        }
        return false;
    };
    defectList.refresh();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM