【问题标题】:Sencha Touch - How to apply different filters to one store for different uses?Sencha Touch - 如何将不同的过滤器应用到一个商店以用于不同的用途?
【发布时间】:2011-10-18 23:39:24
【问题描述】:

只是想知道有没有办法在 Sencha Touch 中处理这个问题。 一个商店,应用不同的过滤器后,商店可以用于不同的地方。

例如:

我有一家商店:

Ext.regModel('abc', {
    fields: [{name: 'tid', type: 'int'}, {name: 'name',  type: 'string'}, {name: 'parent', type: 'int'}]
});

var store = new Ext.data.Store({
     model: 'abc',
     proxy: {
        type: 'ajax',
        url : 'read.php',
        reader: {
            type: 'json',
            root: 'items',
            fields: ['tid', 'name', 'parent']
        }
    },
    autoLoad: true,
});

在 FormPanel 中,它有两个选择域:

items: [
    {
        xtype: 'selectfield',
        name : 'One',
        label: 'Category',
        // store: store, <-- Load the whole store
        store: function(){
            store.filter('name', 'digital'); <-- Load part of store
        },
        displayField: 'name',
        valueField: 'tid'
    },{
        xtype: 'selectfield',
        name : 'Two',
        label: 'subCategory',
        // store: store, <-- Load the whole store
        store: function(){
            store.filter('parent', 1); <-- Load part of store
        },
        displayField: 'name',
        valueField: 'tid'
    },{
    ...}
]

【问题讨论】:

    标签: sencha-touch extjs


    【解决方案1】:

    不用担心,您需要两个商店实例。

    为什么不扩展你的商店,让它像这样预先配置:

    Ext.regModel('abc', {
    fields: [{name: 'tid', type: 'int'}, {name: 'name',  type: 'string'}, {name: 'parent', type: 'int'}]});
    
    MyStore = Ext.extend(Ext.data.Store, {
    constructor: function(config) {
    
    config = Ext.apply({
    
     model: 'abc',
     proxy: {
        type: 'ajax',
        url : 'read.php',
        reader: {
            type: 'json',
            root: 'items',
            fields: ['tid', 'name', 'parent']
        }
    },
    autoLoad: true,
    }, config);
    
    MyStore.superclass.constructor.call(this, config);
    

    }

    })

    然后您可以使用商店的多个实例并根据需要过滤它们:

    items: [
    {
        xtype: 'selectfield',
        name : 'One',
        label: 'Category',
        store: new MyStore(), //1st instance of MyStore
        displayField: 'name',
        valueField: 'tid',
    listeners : {
       'render' : function(){
            this.store.filter('name', 'digital');
        }
    }
    },{
        xtype: 'selectfield',
        name : 'Two',
        label: 'subCategory',
        store: new MyStore(), //2nd instance of MyStore
        displayField: 'name',
        valueField: 'tid',
    listeners : {
       'render' : function(){
            this.store.filter('parent', 1);;
        }
    }
    },{
    ...}
    

    ]

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2012-01-20
      • 1970-01-01
      • 2015-11-10
      相关资源
      最近更新 更多