ExtJS网格过滤 - 同时激活两个

时间:2012-03-23 15:27:38

标签: extjs grid filtering

我有以下代码,可以通过单击按钮从表单中输入的值中过滤网格。问题是第一次激活过滤器时,只有第一个过滤器(displayNameFilter)将包含在请求中。

第二次打开,两个过滤器都将包含在请求中。我该如何解决这个问题?

var nameFilter = grid.filters.getFilter('name');

if (!nameFilter) {
    nameFilter = grid.filters
            .addFilter({
                type : 'string',
                dataIndex : 'name'
            });
}
nameFilter.setValue(Ext.getCmp('name-filter').getValue());

var displayNameFilter = grid.filters.getFilter('displayName');

if (!displayNameFilter) {
    displayNameFilter = grid.filters
            .addFilter({
                type : 'string',
                dataIndex : 'displayName'
            });
}

displayNameFilter.setValue(Ext.getCmp('display-name-filter').getValue());

displayNameFilter.setActive(true, false);
nameFilter.setActive(true, false);

2 个答案:

答案 0 :(得分:2)

我有类似的问题。解决方案有点笨拙,但它适用于我,请注意过滤器变量是在延迟调用中第二次定义(需要):

grid.filters.createFilters();
var nameFilter = grid.filters.getFilter('name');
if (!nameFilter) {
    nameFilter = grid.filters
            .addFilter({
                type : 'string',
                dataIndex : 'name'
            });
}
nameFilter.setActive(true);
var displayNameFilter = grid.filters.getFilter('displayName');
if (!displayNameFilter) {
    displayNameFilter = grid.filters
            .addFilter({
                type : 'string',
                dataIndex : 'displayName'
            });
}
displayNameFilter.setActive(true);
Ext.Function.defer(function() {
    nameFilter = grid.filters.getFilter('name');
    nameFilter.setValue(Ext.getCmp('name-filter').getValue());
    displayNameFilter = grid.filters.getFilter('displayName');
    displayNameFilter.setValue(Ext.getCmp('display-name-filter').getValue());
}, 10);

答案 1 :(得分:2)

这对我有用:

handler : function() {

    var filters = [];

    // get filters from a form...
    var values = filterForm.getValues();
    for (var f in values) {
        var value = values[f];
        if (value) {
            filters.push({ property: f, value: value });
        }
    }

    //...and add all of them to the store used by the grid directly
    if (filters.length) {
        // prevent events firing here...
        gridStore.clearFilter(true);
        // ...because they will fire here
        gridStore.filter(filters);
    } else {
        gridStore.clearFilter();
    }
}