PDA

View Full Version : Get valuefield of a combobox



Skullish
1 Feb 2010, 8:47 AM
Hi,

I have created a combobox with a dynamicly fulled store and i want to get the valuefield from this combobox. I can get easily the displayfield of the selected item of the combobox by using the function getValue() but i can't find the function to get the valuefield.

for example, i have a store fulled with ids and names of persons. i put the id in the valuefield and the name in the displayfield. how can i get the id of the selected item in the combobox?

im using the version 2.2.1

thanks,
Skullish

Edit: I forgot to tell that i tried some other functions like getName(), getId() or getItemId() but i always get the error that they are not a function...

Matthias_WB
1 Feb 2010, 9:56 AM
You must set the valueField-propertie in config of your combobox. Then you can read the value over getValue().

See example: http://www.extjs.com/deploy/dev/docs/?class=Ext.form.ComboBox (http://www.extjs.com/forum/../deploy/dev/docs/?class=Ext.form.ComboBox)


var combo = new Ext.form.ComboBox({
mode: 'local',
store: new Ext.data.ArrayStore({
id: 0,
fields: [
'myId',
'displayText'
],
data: [[1, 'item1'], [2, 'item2']]
}),
valueField: 'myId',
displayField: 'displayText'
});


If that was not the solution, please send your code for define combo-box and store.

Skullish
1 Feb 2010, 10:52 AM
no, that is not the solution. here's my code :


var arrayPersonnes;
var stoPersonnes = new Ext.data.SimpleStore({
fields: [
'id',
'name'
]
});
Ext.Ajax.request({
url : './modele.php',
params : {'demande' : 'getPersons'},
method : 'post',
success : function(response) {
var cbo = document.getElementById("cboComboBox");
//alert(Ext.util.JSON.decode(response.responseText));
var xmlPersonnes = response.responseXML;
arrayPersonnes = xmlPersonnes.getElementsByTagName('person');
//alert(XMLUtils.getTextContentFromNode(arrayPersonnes[0].getElementsByTagName('name')[0]));
for(var Personne = 0; Personne < arrayPersonnes.length; Personne++){
var id = XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('id')[0]);
var name = XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('name')[0]) + " " +
XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('lastname')[0]);
stoPersonnes.loadData([[id, name]], true);
}
}
});
if(!win3){
win3 = new Ext.Window({
id:'win3',
layout:'fit',
width:500,
height:300,
closeAction:'hide',
plain: true,
title:'Troisième fenêtre de configuration',
modal:true,
items: new Ext.Panel({
border:false,
html:'fenêtre 3',
layout:'absolute',
items: [
new Ext.form.ComboBox({
id:'cboComboBox',
typeAhead: true,
triggerAction: 'all',
lazyRender:true,
mode: 'local',
store: stoPersonnes,
valueField: 'id',
displayField: 'name',
x:40,
y:40,
forceSelection:true,
//value:1,
editable:false
}),
new Ext.Panel({
x:40,
y:65,
border:false,
items : [
new Ext.Button({
text:'SelectedValue?',
handler: function(){
var cbo = Ext.get('cboComboBox');
alert("Value: " + cbo.getValue());
}
})
]
})
]
}),

buttons: [{
text:'Précédant',
handler: function(){
win2.show(this);
win3.hide();
}
},{
text:'Suivant',
disabled:true
},{
text: 'Terminer',
handler: function(){
win3.hide();
}
}]
});
}


My combobox displays all the person's names and i can get them by getValue(), but i want to get the id, not the name.

Skullish
1 Feb 2010, 11:56 AM
Sorry if I double post, but my last post doesn't seem to appear on the forum...

the valuefield and displayfield are not the solution, i already have set them.

here is my store definition code :



var arrayPersonnes;
var stoPersonnes = new Ext.data.SimpleStore({
fields: ['id', 'name']
});
Ext.Ajax.request({
url : './modele.php',
params : {'demande' : 'getPersons'},
method : 'post',
success : function(response) {
var cbo = document.getElementById("cboComboBox");
//alert(Ext.util.JSON.decode(response.responseText));
var xmlPersonnes = response.responseXML;
arrayPersonnes = xmlPersonnes.getElementsByTagName('person');
for(var Personne = 0; Personne < arrayPersonnes.length; Personne++){
var id = XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('id')[0]);
var name = XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('name')[0]) + " " +
XMLUtils.getTextContentFromNode(arrayPersonnes[Personne].getElementsByTagName('lastname')[0]);
stoPersonnes.loadData([[id, name]], true);
}
}
});
and here is my combobox code :


new Ext.form.ComboBox({
id:'cboComboBox',
typeAhead: true,
triggerAction: 'all',
lazyRender:true,
mode: 'local',
store: stoPersonnes,
valueField: 'id',
displayField: 'name',
hiddenName: 'id',
x:40,
y:40,
forceSelection:true,
editable:false
})


I am able to retrieve the name displayed by the selected combobox item (so the displayField) but not the valueField (that would be the id). I'm using the getValue() method of the combobox and I don't see other methods that would give me the valueField.

Skullish
2 Feb 2010, 10:14 AM
For the people who got the same error as me (if there is), the solution is to use Ext.getCmp instead of Ext.get to get the combobox and then getValue will return the ValueField instead of the DisplayField.