使用ExtJS从一个属性中的值数组过滤存储 [英] Filter a store with array of values from one property with ExtJS

查看:50
本文介绍了使用ExtJS从一个属性中的值数组过滤存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在组合框上应用约束.目前,它正在半工作.在组合框上,我有以下侦听器:

I'm trying to apply a constraint on combobox. It's half-working at the moment. On the combobox, I have this listener:

[...]
        listeners: {
            'focus': function (combo, value) {
                var orgComboVal = Ext.getCmp('Org1')
                var getOrgValue = orgComboVal.getValue();
                if (typeof getOrgValue !== undefined) {
                    reseaulist.clearFilter(true);
                    for (var q = 0, l = getOrgValue.length; q < l; q++) {
                        reseaulist.filter([
                            {property:'code_organisme', value: getOrgValue[q]}
                        ]);
                    }
                }
            }
        }

Ext.getCmp('Org1')定义了另一个组合框.
orgComboVal.getValue()是单个值时,该过滤器将很好地应用.
但是当它是一个值数组时,例如 ["5","9"] ,它不起作用,应该被过滤的组合框没有显示任何值(所以我猜一个过滤器仍然在应用,但方式不正确.

Ext.getCmp('Org1') defines another combobox.
When orgComboVal.getValue() is a single value, the filter is well applying.
but when it's an array of value, eg ["5", "9"], it's not working and the combobox supposed to be filtered shows no value (so I guess a filter is still applied but in an incorrect way).

我猜是因为reseaulist.filter被多次调用了.我该如何实现?

I guess it's because the reseaulist.filter is called multiple time. How can I achieve this ?

我看到了 filterBy 方法,但是我不知道如何使它工作.

I saw the filterBy method but I don't know how to make it work.

另外,这篇文章很有趣:如何但是一次不能筛选出具有多个值的商店?

Also, this post is interesting : How to filter a store with multiple values at once? but same, can't make it work since

getOrgValue.split(',') 

显示错误

(Object Array has no method split)

有什么秘诀吗?我正在使用ExtJS 4.2.

Any tips ? I'm using ExtJS 4.2.

编辑

感谢@rixo,我做到了.另外,我不得不更改他提供给我的一些代码,因为Org1组合框的值始终是一个数组,即使为空,因此也从未清除存储过滤器.

Thanks to @rixo, I've made it. Also, I had to change some of the code he provided me, because the value of the Org1 combobox was always an array, even if empty, so the store filter was never cleared.

是这里:

            'focus': function (combo, value) {
                var orgComboVal = Ext.getCmp('Org1')
                var values = orgComboVal.getValue();
                console.log(values)
                if (values != null) {
                    reseaulist.clearFilter(false);
                    if (Ext.isArray(values)) {
                        if (0 < values.length) {
                            reseaulist.filterBy(function(record, id) {
                                return Ext.Array.contains(values, record.get('code_organisme'));
                            });
                        } else {
                        reseaulist.clearFilter(true);
                    }   
                    } 
                } 
            }

推荐答案

每个过滤器都一个接一个地应用在先前过滤的数据集上,因此您的代码实现了逻辑AND.这就是为什么所有值都被过滤掉的原因...

Each filter is applied one after the other on the previously filtered data set, so your code implements a logical AND. That's why all values are filtered out...

下面是使用 filterBy 接受数组中任何值的示例:

Here's an example using filterBy to accept any value that is in your array:

function (combo, value) {
    var orgComboVal = Ext.getCmp('Org1')
    var values = orgComboVal.getValue();
    if (values != null) {
        store.clearFilter(false);
        if (Ext.isArray(values)) {
            store.filterBy(function(record, id) {
                return Ext.Array.contains(values, record.get('code_organisme'));
            });
        } else {
            record.get('code_organisme') === values;
        }
    } else {
        store.clearFilter(true);
    }
}

或者您也可以将正则表达式与 filter 方法一起使用:

Or you could also use a regex with the filter method:

function (combo, value) {
    var orgComboVal = Ext.getCmp('Org1')
    var values = orgComboVal.getValue();
    if (values != null) {
        var filterValue = Ext.isArray(values)
            ? new RegExp('^(?:' + Ext.Array.map(values, function(value){return Ext.escapeRe(value)}).join('|') + ')$')
            : values;
        store.clearFilter(false);
        store.filter('code_organisme', filterValue);
    } else {
        store.clearFilter(true);
    }
}

关于您的错误,数组确实没有 split 方法.字符串可以拆分为一个数组.数组在它们的侧面可以连接成字符串...

Concerning your error, arrays indeed don't have a split method. Strings can be split into an array. Arrays, on their side, can be joined into a string...

这篇关于使用ExtJS从一个属性中的值数组过滤存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆