bitdifferent
8 Jun 2007, 5:25 AM
Here's how to pre-select the value that a combobox will display, which took me a while to figure out (credit to anyone who helped me on this, I can't remember who though).
Short answer: use MyCombo.setValue(x) in the handler for the Load event of the DS which the Combo uses.
Long answer:
//set up data for the combo, we'll use an array for simplicity:
var MyComboData = [
[1, "Red"],
[2, "Orange"],
[3, "Yellow"],
[4, "Green"],
[5, "Blue"],
[6, "Indigo"],
[7, "Violet"]
];
//and a Data Store:
var MyComboDataStore = new Ext.data.Store
(
{
proxy: new Ext.data.MemoryProxy (MyComboData),
reader: new Ext.data.ArrayReader
(
{
id: 0 //nb: id for an ArrayReader is the column subscript, NOT the name!
},
[
'ColourID',
'ColourName'
]
)
}
);
Obviously this could be a Json-based combo if you prefer.
Now the combo itself:
var MyCombo = new Ext.form.ComboBox
(
{
//config options here eg typeAhead, triggerAction...
store: MyComboDataStore,
displayField: 'ColourName',
valueField: 'ColourID'
)
);
function WhenMyDSLoads()
{
MyCombo.setValue(4) //Green
}
//and finally
MyComboDataStore.on('load',WhenMyDSLoads);
Hope this helps someone!
Matt S.
Short answer: use MyCombo.setValue(x) in the handler for the Load event of the DS which the Combo uses.
Long answer:
//set up data for the combo, we'll use an array for simplicity:
var MyComboData = [
[1, "Red"],
[2, "Orange"],
[3, "Yellow"],
[4, "Green"],
[5, "Blue"],
[6, "Indigo"],
[7, "Violet"]
];
//and a Data Store:
var MyComboDataStore = new Ext.data.Store
(
{
proxy: new Ext.data.MemoryProxy (MyComboData),
reader: new Ext.data.ArrayReader
(
{
id: 0 //nb: id for an ArrayReader is the column subscript, NOT the name!
},
[
'ColourID',
'ColourName'
]
)
}
);
Obviously this could be a Json-based combo if you prefer.
Now the combo itself:
var MyCombo = new Ext.form.ComboBox
(
{
//config options here eg typeAhead, triggerAction...
store: MyComboDataStore,
displayField: 'ColourName',
valueField: 'ColourID'
)
);
function WhenMyDSLoads()
{
MyCombo.setValue(4) //Green
}
//and finally
MyComboDataStore.on('load',WhenMyDSLoads);
Hope this helps someone!
Matt S.