PDA

View Full Version : How can i make to select a default one?



gigas01
19 Sep 2012, 10:28 AM
Here i have a combo box

In there, it has two options

option1

option2

I want to make the combobox to have been already chosen

option1 for default

How can i make it?




initComponent: function() {
var me = this;
Ext.define('User', {
extend : 'Ext.data.Model',
fields : ['test']
});
var myStore = Ext.create('Ext.data.Store', {
model : User,
data:[{
test: 'option 1'
},
{
test: 'option 2'
}
]
});


Ext.applyIf(me, {
loadMask : true,
stripeRows : true,
store: myStore,
items: [
{
xtype: 'combobox',
fieldLabel: 'Label',
displayField: 'test',
store: myStore,
dataIndex: 'test',
name: 'test',
}
]
});


me.callParent(arguments);
}

mbaird
19 Sep 2012, 10:50 AM
I do something like this:


listeners: {
'afterrender': function(component, args) {
component.setValue('default');
}
}

where 'default' is the key for the item I want selected.

Tim Toady
19 Sep 2012, 11:05 AM
There is also a value property on combos that will do it for you if you know the value
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.Field-cfg-value

gigas01
19 Sep 2012, 11:08 AM
Thanks for the reply, but my requirement is a little different.

I think this setValue is just writing the string on the combobox.

However, i want my data to be displayed

I tried


component.setValue(1);but doesn't work.oh, i also add id on my data for this.

Tim Toady
19 Sep 2012, 11:13 AM
What do you mean by "want my data to be displayed"? Are you listening to an event on the combo?

gigas01
19 Sep 2012, 11:20 AM
my data means what i declared. 'option1' and 'option2'

i want to display 'option1' on the combobox

Tim Toady
19 Sep 2012, 11:28 AM
setValue or the value property require a value. This value is not necessarily what is diplayed in the box (the displayField). If the valueField on the combo is something like 'id' you would use the id for the record you want. If the valueField is not set on the combo its not going to work.

edit--
so if your combo's valueField is 'id' and your combo's store contains a record that is
{
id: 7,
text: 'whatever'
}

you can say yourCombo.setValue( 7 )

gigas01
19 Sep 2012, 12:12 PM
Thank you

i utilized your guys advice and succeeded :)