PDA

View Full Version : How to get combobox display 1st element ?



user14
21 Dec 2011, 5:13 PM
This is working code : panel with combobox apeears. Clicking on
combbox gives me items from servlet. BUT INITALLY combobox is blank.
I'd like to see 1-st item in combobox right after panel with combo appears on screen.
Please help!



Ext.define('Test.store.ComboStoreServlet', {
extend: 'Ext.data.Store',
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent( [ Ext.apply ({
autoLoad: true,
storeId: 'MyJsonStore1',
proxy: {
type: 'ajax',
url: 'http://localhost:8080/Test/Test', reader: {
type: 'json',
root: 'rows'
} }, fields: [ {name: 'id'},{name: 'name'}
]
}, cfg)]);
}});

Ext.application({ name: 'Test',
launch: function() {Ext.create('Test.store.ComboStoreServlet');
Ext.create('Ext.panel.Panel', { layout: 'absolute',
id: 'panel_id', height: 300,
width: 500,
title:'App launch 1',
renderTo: Ext.getBody(),
items: [ {
xtype: 'combo',
fieldLabel: 'Label',
displayField: 'name',
id:'myc',
store: 'MyJsonStore1',
valueField: 'id',
x: 20,
y: 40,
listeners:{
beforerender:function(combo,opts){
alert(combo.store);
}
} } ] });
}
});

mitchellsimoens
21 Dec 2011, 7:52 PM
Please post in the appropriate forum. I have moved this to the Ext JS 4 Q&A forum.

mitchellsimoens
21 Dec 2011, 7:52 PM
So you want the first item in the store to be the value of the combobox?

user14
22 Dec 2011, 6:05 AM
So you want the first item in the store to be the value of the combobox?
Yes , I want somebody to help with this

mitchellsimoens
22 Dec 2011, 6:17 AM
Yes , I want somebody to help with this

Um... I am, just needed to ask a question first.

You have to get the first item in the store via:


store.getAt(0)

Then you have to set the value of the combo with the field value:


combo.setValue(rec.get(combo.valueField))

user14
22 Dec 2011, 6:31 AM
Um... I am, just needed to ask a question first.

You have to get the first item in the store via:


store.getAt(0)

Then you have to set the value of the combo with the field value:


combo.setValue(rec.get(combo.valueField))
Here is what I added to the listeners ( in code above): and it still the same ( not displaying 1-st element)
listeners:{

beforerender:function(combo,opts){
store.getAt(0);
combo.setValue(rec.get(combo.valueField));
}
}

My servlet (I see it ) replies with :
TestServlet doGet {rows:[{"id":"1","name":"John"},{"id":"2","name":"Smith"} ]}
That's what i see now
So I'd like 30254
to see when my app loads
30255

mitchellsimoens
22 Dec 2011, 6:32 AM
You didn't define the rec variable from the getAt call on the store

user14
22 Dec 2011, 6:37 AM
that is not working also:
beforerender:function(combo,opts){
rec=combo.store.getAt(0);
combo.setValue(rec.get(combo.valueField));
}
}

mitchellsimoens
22 Dec 2011, 6:40 AM
Is the store loaded? Any errors?

user14
22 Dec 2011, 6:45 AM
I see my servlet responds to request.
that is not working also
combo.store.load();
rec=combo.store.getAt(0);
combo.setValue(rec.get(combo.valueField));
It seems to be simple task - but ??? to resolve it ?

mitchellsimoens
22 Dec 2011, 6:52 AM
if you are doing the getAt right after load, the store isn't loaded at that point. Loading is asynchronous so you have to do this code after the store has loaded


store.on('load', function(store) {
var rec = store.getAt(0);
combo.setValue(rec.get(combo.valueField));
}, store, { single : true });

user14
22 Dec 2011, 7:25 AM
Sorry , may be it becomes ??? , but I got confused :
I put that code into listeners ( as u can c in code above ) I defined store and created it and panel
with combo in application. Combo has listeners where I put that code ( or should I put it somewhere else)
beforerender:function(combo,opts){
store.on('load', function(store) {
var rec = store.getAt(0);
combo.setValue(rec.get(combo.valueField));
}, store, { single : true });


}
}
:( .. but still getting nothing

mitchellsimoens
22 Dec 2011, 7:30 AM
Ok... you need to stop just copy and pasting code. You need to look at what the code snippets are doing and understand it. In your beforerender listener, is the variable 'store' defined when you do store.on? Or do you need to get the store from the combo parameter in that function?