PDA

View Full Version : try to set the value of the fieldLabel dynamicaly



flamant
1 Sep 2010, 2:25 AM
Hi,
I try to set the value of the fieldLabel (of a textField) dynamicaly based on an ajax request and it doesn't work

Here is the code :



var conn = new Ext.data.Connection();
conn.request({
url: "<c:url value="/secure/getSubjectRangeNumber.htm"/>",
method: 'GET',
params: { centerId: id },
success: function(response, opts) {
var results = Ext.util.JSON.decode(response.responseText);
var result = results.records[0].label;
var subjectNumber = Ext.getCmp("subjectNumber");
subjectNumber.fieldLabel = '<spring:message code="subject.number"/>'+' '+result;
}
},
failure: function(response, opts) {

}
});
and my Textfield :



{
id : "subjectNumber",
xtype : "textfield",
name : "subjectNumber",
readOnly: isReadOnly,
allowBlank: false,
width : 400,
helpText: '<spring:message code="subject.number.help"/>'
},
I didn't find another way to do it. Thank you in advance for your answers

fabio.parra
1 Sep 2010, 4:02 AM
You can use this override to set fieldlabel:



Ext.override(Ext.form.Field, {
setFieldLabel : function(text) {
var label = this.getLabelEl();
if(label){
label.update(text);
this.fieldLabel = text;
}
},

setLabelWidth : function(width){
var label = this.getLabelEl();
if(label){
label.setWidth(width);
}
},

getLabelEl : function(){
if (this.rendered){
var l = this.el.up('.x-form-item', 10, true);
if(l){
return l.child('.x-form-item-label');
}
}
return null;
}

});


and then just call:



subjectNumber.setFieldLabel('myCoolText');

flamant
1 Sep 2010, 11:52 AM
Hi fabio and thank you for your answer. it works fine

Animal
1 Sep 2010, 11:57 AM
http://dev.sencha.com/deploy/dev/docs/index.html?class=Ext.form.Field&member=label

flamant
12 Sep 2010, 2:01 AM
Hi Animal and Thank you for your answer. I finally used The method that fabio.parra suggested me

Condor
12 Sep 2010, 8:27 AM
That code is for a pretty old Ext version. As Animal already suggested you can just use 'label' instead of the whole getLabelEl function.