过滤ViewModel存储并实现到ExtJS Panel的正确方法是什么?

时间:2018-03-01 10:00:48

标签: javascript extjs filter viewmodel store

通过ViewModel商店我获取此JSON数据;

{
    "success": true,
    "msg": "OK",
    "count": 2,
    "data": [
        {
            "firstname": "BLA",
            "lastname": "BLALA",
            "isactive": true,
            ...
        },
        {
            "firstname": "BLAAA",
            "lastname": "BLALAAA",
            "isactive": false,
            ...
        }

我在一个panel上有两个网格,其中一个仅使用isactive: true字段加载数据,其他网格只会加载false那么我需要在何处以及如何过滤商店以将指定数据加载到网格

这是VM;

Ext.define('MyApp.view.ListingVM', {
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.listing',

    requires: [...],
    reference: 'loyaltyvm',

    stores: {

        // Should I define the filter on here?
        bonusTrans: {
            storeId: 'bonusTrans',
            // reference: 'bonusTrans',
            model: 'MyApp.model.BonusTrans',
            autoLoad: true,
            session: true,
            pageSize: MyApp.Globals.LIST_PAGE_SIZE
        }
    }
});

这是panel网格示例,其中定义了两个网格。我已经尝试了几种方法来获取store并过滤但不能成功;

getColumns: function () {
    var me = this;

    var panelItems = [
        {
            xtype: 'container',
            layout: {type: 'hbox', align: 'stretch', pack: 'start'},
            items: [
                    xtype: 'bonustrans',
                    flex: 1,
                    title: 'Current Bonus',
                    getListCols: function () {
                        var me = this;
                        debugger;
                        // var activeStore = Ext.getStore('bonusTrans');
                        // var activeStore = me.viewModel.get('bonusTrans');
                        // var view = me.getView();
                        // var vm = view.getViewModel();
                        // var vm.getStore('bonusTrans')
                        // var activeStore = me.getViewModel().getStore('bonusTrans');

                        var activeStore = me.getViewModel('loyaltyvm').getStores('bonusTrans');
                        activeStore.filter('isactive', 'true');

                        var listCols = [
                            {
                                xtype: 'firstnamecol',
                                flex: 1
                            },
                            {
                                xtype: 'checkoutcol'
                            },
                            {
                                xtype: 'bonustotalcol'
                            }
                        ];
                        return listCols;
                    }
                    //... other Grid is just defined below of this line and it should loads data only with 'isactive' field is false.

2 个答案:

答案 0 :(得分:2)

使用链式商店,fiddle

Ext.application({
    name : 'Fiddle',

    launch : function() {
        new Ext.container.Viewport({
            layout: {
                type: 'hbox',
                align: 'stretch'
            },
            viewModel: {
                stores: {
                    everything: {
                        autoLoad: true,
                        proxy: {
                            type: 'ajax',
                            url: 'data1.json'
                        }
                    },
                    active: {
                        type: 'chained',
                        source: '{everything}',
                        filters: [{
                            property: 'active',
                            value: true
                        }]
                    },
                    inactive: {
                        type: 'chained',
                        source: '{everything}',
                        filters: [{
                            property: 'active',
                            value: false
                        }]
                    }
                }
            },
            items: [{
                flex: 1,
                xtype: 'gridpanel',
                title: 'Active',
                bind: '{active}',
                columns: [{
                    dataIndex: 'name'
                }]
            }, {
                flex: 1,
                xtype: 'gridpanel',
                title: 'Inactive',
                bind: '{inactive}',
                columns: [{
                    dataIndex: 'name'
                }]
            }]
        });
    }
});

答案 1 :(得分:2)

连锁店的方式肯定是最好的, here you can see a working fiddle on classic

这是代码:

Ext.application({
name: 'Fiddle',

launch: function () {

    var storeAll = Ext.create('Ext.data.Store', {
            storeId: 'storeAll',
            fields: [{
                name: 'firstname'
            }, {
                name: 'lastname'
            }, {
                name: 'active'
            }],
            data: [{
                firstname: 'test1',
                lastname: 'test1',
                active: true
            }, {
                firstname: 'test2',
                lastname: 'test2',
                active: true
            }, {
                firstname: 'test3',
                lastname: 'test3',
                active: false
            }]
        }),
        chainedStoreActive = Ext.create('Ext.data.ChainedStore', {
            source: storeAll,
            filters: [{
                property: 'active',
                value: true
            }]
        }),
        chainedStoreNoActive = Ext.create('Ext.data.ChainedStore', {
            source: storeAll,
            filters: [{
                property: 'active',
                value: false
            }]
        });

    Ext.create({
        xtype: 'viewport',
        layout: {
            type: 'vbox',
            align: 'stretch'
        },
        items: [{
            xtype: 'gridpanel',
            title: 'grid ALL',
            store: storeAll,
            columns: [{
                text: 'First Name',
                dataIndex: 'firstname'
            }, {
                text: 'Last Name',
                dataIndex: 'lastname'
            }],
            flex: 1
        }, {
            xtype: 'gridpanel',
            title: 'grid active',
            store: chainedStoreActive,
            columns: [{
                text: 'First Name',
                dataIndex: 'firstname'
            }, {
                text: 'Last Name',
                dataIndex: 'lastname'
            }],
            flex: 1
        }, {
            xtype: 'gridpanel',
            title: 'grid inactive',
            store: chainedStoreNoActive,
            columns: [{
                text: 'First Name',
                dataIndex: 'firstname'
            }, {
                text: 'Last Name',
                dataIndex: 'lastname'
            }],
            flex: 1
        }],

        renderTo: Ext.getBody()
    });
}

});

全局或“allelements”商店需要是一个全局商店,链接的商店可以在视图的视图模型中创建。