PDA

View Full Version : how to set a boolean value to a radiogroup



xunxin
6 Apr 2012, 12:54 AM
i call the loadRecord method of the form which contains a radiogroup(male and female),but the radio in the radiogroup don't checked by the loaded value.

anyone can help me? thanks in advance.

xunxin
6 Apr 2012, 12:57 AM
{ fieldLabel: 'gendar',
xtype: 'radiogroup',
allowBlank: false,
defaults: {
flex: 1,
name : 'gendar'
},
layout: 'hbox',
items: [
{
boxLabel: 'male',
inputValue: 'true',
checked: true
},
{
boxLabel: 'female',
inputValue:'false'
}
]
}

xunxin
6 Apr 2012, 5:47 PM
i read the document many times, but have no idea how to set it.

anyone can help me?

xunxin
6 Apr 2012, 8:03 PM
i solved it by override the setValue method of RadioGroup.

i think it is bug. the override code is following:


Ext.define('my.form.RadioGroup',{ override : 'Ext.form.RadioGroup',

setValue: function(value) {
var me = this,name=me.name,wrapValue={};
if(!Ext.isObject(value)){//the value will not be a object if it loaded from a form
wrapValue[name]=value;
value = wrapValue;
}
return me.callParent(arguments);
}
});

the config


{ fieldLabel: 'gendar',
xtype: 'radiogroup',
allowBlank: false,
name : 'gendar',// it is neccessary
defaults: {
flex: 1,
name : 'gendar' // it is neccessary
},
layout: 'hbox',
items: [
{
boxLabel: 'male',
inputValue: true,
checked: true
},
{
boxLabel: 'female',
inputValue: false
}
]}

siebe vos
31 Oct 2013, 10:30 AM
The problem for this one is actually lying in the radiobutton itself (not in the group). Since the setValue of the radiobutton sees a boolean the same as checked / unchecked. This is because the radio is inherited from the checkbox (I think). See if (Ext.isBoolean(v)) condition in http://docs.sencha.com/extjs/4.0.7/source/Radio.html#Ext-form-field-Radio-method-setValue

A problem with the override above occurs when checked is not used, but the default value should be false (so female) and is set to false using the loadRecord. In this situation the override above only works when the boolean is forced to a string and the inputValues of the radios is also defined as a boolean strings:



Ext.define('Bc.overrides.form.RadioGroup', {
override: 'Ext.form.RadioGroup',
setValue: function (value) {
var me = this,
name = me.name,
wrapValue = {};
if (!Ext.isObject(value)) { //the value will not be a object if it loaded from a form
//force boolean to string because of false gives problems with radiobutton since it extends a checkbox and its setValue sees false as a the check boolean
//see setValue of the
if (Ext.isBoolean(value)) value = value.toString();
wrapValue[name] = value;
value = wrapValue;
}
return me.callParent(arguments);
}
});

The config:

{
fieldLabel: 'gendar',
xtype: 'radiogroup',
allowBlank: false,
name: 'gendar', // it is neccessary
defaults: {
flex: 1,
name: 'gendar' // it is neccessary
},
layout: 'hbox',
items: [{
boxLabel: 'male',
inputValue: 'true', //force as boolean string
}, {
boxLabel: 'female',
inputValue: 'false' //force as boolean string
}]
}