【问题标题】:Store with MixedCollection data使用 MixedCollection 数据存储
【发布时间】:2012-12-02 14:30:55
【问题描述】:

是否可以在 ExtJS 4.1.x 中执行此过程?

var myMixedCollection = myStore.queryBy(...);
var anotherStore = Ext.create('Ext.data.Store', { data: myMixedCollection, ... });
var myGrid = Ext.create('Ext.grid.Panel', { store: anotherStore, ... });

因为我的网格不显示任何内容或仅显示一个空行。
当我记录我的myMixedCollection 时,所有数据都在这里,但是当我用 Firebug 打开我的anotherStore 时,我可以看到我的数据存储中只有一个空行。

【问题讨论】:

    标签: extjs grid extjs4 store extjs4.1


    【解决方案1】:

    myMixedCollection 将是记录(模型实例)的集合,只要新商店具有相同的模型集,这将起作用!所以答案是是的

    好吧,您肯定需要在 myMixedCollection 实例上调用 getRange()

    这是一个工作示例

     // Set up a model to use in our Store
     Ext.define('Simpson', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'name', type: 'string'},
             {name: 'email', type: 'string'},
             {name: 'phone', type: 'string'}
         ]
     });
    
    var s1 = Ext.create('Ext.data.Store', {
        model:'Simpson',
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224"  },
            { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244"  },
            { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254"  }
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    var mixed = s1.queryBy(function(rec){
         if(rec.data.name == 'Lisa')
             return true;
    });
    
    var s1 = Ext.create('Ext.data.Store', {
        model:'Simpson',
        storeId:'simpsonsStore2',
        fields:['name', 'email', 'phone'],
        data: mixed.getRange(),
        proxy: {
            type: 'memory',
            reader: {
                type: 'json'
            }
        }
    });
    
    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore2'),
        columns: [
            { text: 'Name',  dataIndex: 'name' },
            { text: 'Email', dataIndex: 'email', flex: 1 },
            { text: 'Phone', dataIndex: 'phone' }
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });
    

    还有 JSFiddle

    【讨论】:

    • 是的,它有效,我忘记使用 getRange() 方法。谢谢你。
    【解决方案2】:

    是的,有可能。

    试试这个:

    //this is the model we will be using in the store
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id',    type: 'int'},
            {name: 'name',  type: 'string'},
            {name: 'phone', type: 'string', mapping: 'phoneNumber'}
        ]
    });
    
    var data = new Ext.util.MixedCollection();
    data.add('key1', {
        id: 1,
        name: 'Ed Spencer',
        phoneNumber: '555 1234'
    });
    data.add('key2', {
        id: 2,
        name: 'Abe Elias',
        phoneNumber: '666 1234'
    });
    
    //note how we set the 'root' in the reader to match the data structure above
    var store = Ext.create('Ext.data.Store', {
        model: 'User',
        data : data.items,
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'users'
            }
        }
    });
    
    store.each(function(record){
        console.log(record.get("name"));
    });
    

    ​你可以在这里看到它在 jsfiddle 上工作:http://jsfiddle.net/lontivero/Lf9yv/1/

    【讨论】:

    • 大声笑,那么这里就是证明 ;-) +1 对于所有相同的答案。不知道如果您的反击已经足够了,但是如果您给分数小费,您可以看到赞成/反对的比率。你的现在是 1/1 ;) 最后但同样重要的是,不,我没有。
    • awww,-1 巨魔又要来了?这是一个 +1 让他们离开 :)
    猜你喜欢
    • 2017-04-28
    • 2012-01-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2016-07-29
    相关资源
    最近更新 更多