2012-12-14 37 views
5

Có thể thực hiện quy trình này trong ExtJS 4.1.x không?Lưu trữ với dữ liệu MixedCollection

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

Vì lưới của tôi không hiển thị hoặc chỉ một dòng trống.
Khi tôi đăng nhập myMixedCollection của mình, không có vấn đề gì về dữ liệu ở đây nhưng khi tôi mở anotherStore bằng Firebug tôi có thể thấy chỉ có một dòng trống trong kho dữ liệu của tôi.

Trả lời

7

myMixedCollection sẽ là một tập hợp các bản ghi (trường hợp mẫu) và miễn là cửa hàng mới có cùng mô hình thiết lập, điều này sẽ hoạt động! Vì vậy, câu trả lời là

Vâng, chắc chắn bạn cần phải gọi getRange() trên myMixedCollection dụ

Dưới đây là một ví dụ làm việc

// 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":"[email protected]", "phone":"555-111-1224" }, 
     { 'name': 'Bart', "email":"[email protected]", "phone":"555-222-1234" }, 
     { 'name': 'Homer', "email":"[email protected]", "phone":"555-222-1244" }, 
     { 'name': 'Marge', "email":"[email protected]", "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

+0

Và nguyện downvoter xin vui lòng ghi rõ lý do tại sao ông đã downvoted? – sra

+0

Có nó hoạt động, tôi quên sử dụng phương thức getRange(). Thx sra. – dgedge03

+2

các troll bị loại bỏ :) – dbrin

2

Có, điều đó là có thể.

Hãy thử điều này:

//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")); 
}); 

Bạn có thể nhìn thấy nó Woking trên jsfiddle đây: http://jsfiddle.net/lontivero/Lf9yv/1/

+0

chuyện gì đã xảy ra !? tại sao -1? – lontivero

+0

Lol, vì vậy đây là bằng chứng sau đó ;-) +1 cho tất cả các câu trả lời tương tự. Dunno nếu việc bồi dưỡng của bạn đã đủ rồi, nhưng bạn có thể thấy tỷ lệ upvote/downvote nếu bạn tip vào điểm số. Của bạn bây giờ là 1/1;) cuối cùng nhưng không kém phần quan trọng, không có tôi không. – sra

+0

awww, -1 troll là về một lần nữa? đây là +1 để khiến họ biến mất :) – dbrin

Các vấn đề liên quan