javascript - SAPUi5 Radio Button Selected Text - Stack Overflow

I have a radio button in my XMLview as shown below. The data is bound from the model. My requirement is

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

It 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745152183a4613917.html

相关推荐

  • javascript - SAPUi5 Radio Button Selected Text - Stack Overflow

    I have a radio button in my XMLview as shown below. The data is bound from the model. My requirement is

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信