admin管理员组文章数量:1415467
I have a radio button in my XMLview as shown below. The data is bound from the model. My requirement is to validate the radio buttons after the data binding. Below is my controller code. i am trying to read the value of the selectedButton text in my controllers AfterRendering method.
XML View:
<RadioButtonGroup id="rbgtype" >
<RadioButton text="Yes" selected="{/rbgtype}"/>
<RadioButton text="No" selected="{=!${/rbgtype}}"/> </RadioButtonGroup>
Controller
onAfterRendering: function () {
var that = this; console.log("rbgtype"+that.getView().byId("rbgtype").getSelectedButton().getText());}
Strangely I am always getting the value "Yes" , irrespective of selected value. Can some one advise where I am doing wrong please.
I have a radio button in my XMLview as shown below. The data is bound from the model. My requirement is to validate the radio buttons after the data binding. Below is my controller code. i am trying to read the value of the selectedButton text in my controllers AfterRendering method.
XML View:
<RadioButtonGroup id="rbgtype" >
<RadioButton text="Yes" selected="{/rbgtype}"/>
<RadioButton text="No" selected="{=!${/rbgtype}}"/> </RadioButtonGroup>
Controller
onAfterRendering: function () {
var that = this; console.log("rbgtype"+that.getView().byId("rbgtype").getSelectedButton().getText());}
Strangely I am always getting the value "Yes" , irrespective of selected value. Can some one advise where I am doing wrong please.
Share Improve this question asked Sep 11, 2018 at 22:53 lampstandlampstand 1113 silver badges15 bronze badges2 Answers
Reset to default 4It does that because the RadioButtonGroup doesn't initially have a property set selectedIndex
. It seems to fail the getSelectedButton
method whenever selectedIndex
isn't set.
Maybe you can work around this issue like this? Hope this helps.
View
<RadioButtonGroup id="rbgtype" selectedIndex ="{/rbgtype}">
<RadioButton text="Yes" />
<RadioButton text="No"/>
</RadioButtonGroup>
Controller
onInit: function() {
var type = false;
var oModel = new JSONModel({
rbgtype: type ? 0 : 1
});
this.getView().setModel(oModel);
},
onAfterRendering: function() {
sap.m.MessageToast.show("rbgtype: " + this.byId("rbgtype").getSelectedButton().getText());
},
VIEW 2
<RadioButtonGroup id="rbgtype" selectedIndex ="{= ${rbgtype} ? 0 : 1 }">
<RadioButton text="Yes" />
<RadioButton text="No"/>
</RadioButtonGroup>
Please check your xml view radionbuttongroup aggregation and binding
<RadioButtonGroup id="rbgtype" >
<buttons>
<RadioButton text="Yes" selected="{/rbgtype}"/>
<RadioButton text="No" selected="{=!${/rbgtype}}"/> </RadioButtonGroup>
</buttons>
</RadioButtonGroup>
Once you fix it then you can add an event delegate for onAfterRendering in onInit function of the controller
see example below
var rbGrp= this.getView().byId("rbgtype");
rbGrp.addEventDelegate({
onAfterRendering:function(){/*write your code here*/}
}, this);
Hope this helps. Any issues let me know
本文标签: javascriptSAPUi5 Radio Button Selected TextStack Overflow
版权声明:本文标题:javascript - SAPUi5 Radio Button Selected Text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745152183a2644969.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论