EXTJS 4 - 网格过滤或存储过滤器清除商店中的现有过滤器

时间:2015-02-18 08:00:44

标签: javascript extjs extjs4 extjs4.1 extjs4.2

我遇到过网格过滤器正在清除商店中现有过滤器的问题。

基本上,我有两个网格在彼此之上,当用户点击顶部网格时,数据被过滤到第二个网格,它是发生问题的第二个网格。

我通过小提琴创建了一个简单的问题示例

https://fiddle.sencha.com/#fiddle/if0

备注

需要注意的是,我通过record.beginEdit()命令“展平”嵌套的JSON自定义字段,以便可以在网格中显示此数据。我使用的是remoteFilter: false,如果设置为remoteFilter: true,则过滤器部分有效,但remoteFilter: true会通过record.beginEdit();

销毁嵌套数据集

如何重现

1)如果您在列Name中应用上面的网格过滤器并选择Homer,则网格过滤器将清除第113行中应用于商店的过滤器,现在显示两个荷马记录。有两个荷马记录,但每个记录都有不同的summaryId值。

store.filter([{id: 'summaryId', property: 'summaryId', value:1, exactMatch: true}]);

2)如果取消选择过滤器,则不会保留第113行中应用于商店的过滤器,现在会显示所有记录。

1 个答案:

答案 0 :(得分:1)

如下所示更改您的商店:

store = Ext.create('Filter.data.Store', {
    data: data,
    filters: [{
        property: 'summaryId',
        value: '1'
    }]
});

查看您之前演示的this演示...

编辑: 试试这个代码,这是一个非常复杂的方法来实现这个目标。所以,你需要一个像按钮一样过滤的触发器,这可能会给你一点帮助。我编辑了我的Demo Too,看看它。

/*global Ext:false */
Ext.Loader.setPath({

});

Ext.require(['*', 'Ext.ux.grid.FiltersFeature']);

Ext.define('Filter.data.Store', {
    extend: 'Ext.data.Store',
    id: 'simpsonsStore',
    autoLoad: true,
    remoteFilter: false,
    fields: [{
        name: 'name'
    }, {
        name: 'email'
    }, {
        name: 'phone'
    }, {
        name: 'summaryId'
    }, {
        name: 'customFields'
    }, {
        name: 'customFieldId129'
    }, {
        name: 'customFieldId130'
    }],
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});


Ext.onReady(function() {

    var filters = {
        ftype: 'filters',
        encode: false,
        local: true
    };

    data = {
        "total": 5,
        "items": [{
            'id': 1,
            'summaryId': 1,
            'name': 'Lisa',
            "email": "lisa@simpsons.com",
            "phone": "555-222-1234",
            "customFields": [{
                "customFieldId": 129, // there can be multiple customfields
                "customFieldValue": "Bob"
            }, {
                "customFieldId": 130, // there can be multiple customfields
                "customFieldValue": "Smith"
            }, ]
        }, {
            'id': 2,
            'summaryId': 1,
            'name': 'Bart',
            "email": "bart@simpsons.com",
            "phone": "555-222-1244",
            "customFields": []
        }, {
            'id': 3,
            'summaryId': 1,
            'name': 'Homer',
            "email": "home@simpsons.com",
            "phone": "555-222-1244",
            "customFields": []
        }, {
            'id': 4,
            'summaryId': 2,
            'name': 'Fred',
            "email": "fred@simpsons.com",
            "phone": "555-222-1264",
            "customFields": []
        }, {
            'id': 5,
            'summaryId': 2,
            'name': 'Homer',
            "email": "HomerWork@simpsons.com",
            "phone": "555-222-1264",
            "customFields": []
        }]
    },

    store = Ext.create('Filter.data.Store', {
        data: data,
        //filters: [{
        //    property: 'summaryId',
        //    value: '1'
        //}]
    });

    store.load({
        callback: function() {
            for (var i = 0; i < store.data.length; i++) {
                var record = store.getAt(i);
                var customFieldsData = record.data.customFields;
                if (customFieldsData.length > 0) {
                    for (var j = 0; j < customFieldsData.length; j++) {
                        record.beginEdit();
                        store.filter([{property: 'summaryId',value: '1'}]);
                        record.set('customFieldId' + customFieldsData[j].customFieldId, customFieldsData[j].customFieldValue);
                        record.endEdit(true);
                    }
                }
                //store.sync();
            }
        }
    });

    // apply custom fields
    // the custom field code is an example, in short , nested data
    // and need to use the record function for each record so that the
    // grid can see the record


    //store.filter([{property: 'summaryId',value: '1'}]);
    var btn = Ext.create('Ext.Button', {
        text: 'Click me For filter summaryId',
        renderTo: Ext.getBody(),
        handler: function() {
            if(store.isFiltered()){
                //var data = Ext.encode(grid.filters.getFilterData());
                var data1 = grid.filters.getFilterData();
                store.filter([{property: 'summaryId',value: '1'}]);
                store.filter([{property: data1[0].field,value: data1[0].data.value}]);
                //Ext.Msg.alert('All Filter Data',data1[0].field + " " + data1[0].data.value);
                //Ext.Msg.alert('All Filter Data',data);
            }
        }
    });

    var grid = Ext.create('Ext.grid.Panel', {
        xtype: 'gridpanel',
        title: 'filter is on summaryId 1 - Selecting Homer returns two records instead of one',
        store: store,
        features: [filters],
        columns: [{
            header: 'Name',
            dataIndex: 'name',
            filter: {
                type: 'list',
                options: ['Homer']
            }
        }, {
            header: 'summaryId',
            dataIndex: 'summaryId'
        }, {
            header: 'customField1',
            dataIndex: 'customFieldId129' //hardcoded for fiddler example
        }, {
            header: 'customField2',
            dataIndex: 'customFieldId130' //hardcoded for fiddler example
        }],
        width: 700,
        height: 500,
        renderTo: Ext.getBody()
    });
});