2012-07-22 33 views
0

我有一个grouped Checkbox,它有4 checkboxes使用复选框过滤 - 逻辑问题

当用户选择复选框时,我需要根据文本框的值过滤数据存储。当我选择2个文本框时,我得到["Albert","Francis"]的输出,并且当我仅选择第一个文本框时,我得到["Albert"]等等。

现在,我需要知道如何过滤这个?

onCheckBoxGroupChange : function (field,newValue,oldValue,options) { 
var chkv=newValue.chb; 
console.log (chkv); 
var st= Ext.getStore('People'); 
     st.on('load', function() { 
      st.filter({ 
       filterFn: function(rec) {     
        return rec.get('name') == chkv; 

我遇到的问题是,当我选择这两个CheckBoxeschkv成为["Albert","Francis"](与数组一样),因此我无法返回值,因为rec.get('name') == chkv;不寻找一个阵列。

有人可以帮助我吗?

回答

0

什么:

filterFn: function(rec) { 
    return Ext.Array.contains(chkv, rec.get('name')); 
} 

我要确保你知道你不必重装店,如果你想过滤。它看起来就是你在上面提供的代码片段中所做的。

还有其他的问题其实,你的onCheckBoxGroupChange处理程序会怎么做(如果你离开它,你拥有它上面的方法),如下:

每次用户点击一个复选框,它首先会重新加载从服务器存储,然后将选定的过滤器添加到商店的过滤器,然后通过已保存的过滤器过滤商店。

例如为:

用户点击 “伟业” 复选框

Store reloads 
Store adds `["Albert"]` filter function 
Store performs filtering with `["Albert"]` filter function 

用户点击 “弗朗西斯” 复选框

Store reloads 
Store adds `["Albert", "Francis"]` filter function 
Store performs filtering with `["Albert"]` filter function 
Store performs filtering with `["Albert", "Francis"]` filter function 

用户unclicks “伟业” 复选框

Store reloads 
Store adds `["Francis"]` filter function 
Store performs filtering with `["Albert"]` filter function 
Store performs filtering with `["Albert", "Francis"]` filter function 
Store performs filtering with `["Francis"]` filter function 

您将逐渐获得越来越多的过滤器。此外不必要地重新加载商店很多次。我不认为这是你的目标。

您应该查看关于datastore filtering的文档以获取更多关于如何操作的数据。

另外,在你的问题中,你说当你选择两个复选框时,你有这个问题,因为它返回一个数组。根据你的问题的文字,当你选择一个复选框时(["Albert"]),你也会得到一个数组。它只有一个项目,但它是一个数组。

这就是为什么我的解决方案(它需要你的chkv变量是一个数组)才能工作。