PDA

View Full Version : ComboBox from Objects instead of array



bowa
16 Oct 2007, 7:05 AM
Ext.myData.array1 = [
['AL', 'Alabama'],
['AK', 'Alaska'],
['AZ', 'Arizona'],
['AR', 'Arkansas'],
['CA', 'California'],
['CO', 'Colorado'],
['CT', 'Connecticut']
];

var myStore = new Ext.data.SimpleStore({
fields: ['short','name'],
data: Ext.myData.array1
});

var myCombo = new Ext.form.ComboBox({
store: paperStore,
displayField: 'name',
triggerAction: 'all',
selectOnFocus: true,
mode:'local',
grow: true,
editable: false,
emptyText: 'choose ...'
});

myCombo.applyTo('test-combo');

The above code works, the data is a simple array

Now in fact i get my data from a DWR call (and i tried with a DWRProxy and that worked smoothly)

But because i have to use the data in multiple combo boxes and i don't want to make an ajax roundtrip for each of them, i store the response locally.

For the example i just created an object to simplify things.



Ext.myData.object1 = [
{name:'One', child: {id:1, age: 3, color: 'yellow'}},
{name:'two', child: {id:6, age: 12, color: 'blue'}},
{name:'three', child: {id:5, age: 9, color: 'red'}}
];

var myStore = new Ext.data.SimpleStore({
fields: ['short','name'],
data: Ext.myData.object1
});

var myCombo = new Ext.form.ComboBox({
store: paperStore,
displayField: 'name',
triggerAction: 'all',
selectOnFocus: true,
mode:'local',
grow: true,
editable: false,
emptyText: 'choose ...'
});

myCombo.applyTo('test-combo');

Now this does not give any errors, there are three 'empty' elements in my combo box list ...
What is the correct way to map an object into a Store?

Air_Mike
16 Oct 2007, 7:20 AM
That store won't work. I have it setup like this:


var my_data = [{"ID":"7","OPIS":"Autorizacija Bank Card"},{"ID":"4","OPIS":"Brokeri"},{"ID":"5","OPIS":"Kartice"}];

var ds = new Ext.data.Store({
proxy: new Ext.data.MemoryProxy(my_data),
reader: new Ext.data.JsonReader({}, [
{name: 'ID'}, //mapping to object
{name: 'OPIS'} //mapping to object
])
});
ds.load();

bowa
16 Oct 2007, 7:33 AM
that doesn't work for me :s

i was thinking

if it works with DWR ...


var pReader = new Ext.data.ObjectReader(
{id:'name'}, [
{name:'name',mapping:'name'},
{name:'types',mapping:'pageTypes'}
]
);

var pStore = new Ext.data.Store({
proxy: new Ext.data.DWRProxy(TreeHelper.getPaperGroupsForColor,[]),
reader: pReader
});

then why would it not work with the same ObjectReader ?


var paperReader = new Ext.data.ObjectReader(
{id:'name'}, [
{name:'name',mapping:'name'},
{name:'types',mapping:'pageTypes'}
]
);

var paperStore = new Ext.data.Store({
proxy: new Ext.data.MemoryProxy(Ext.myData.color),
reader: paperReader
});

with Ext.myData.color being the data returned by the TreeHelper.getPaperGroupsForColor() call.