简体   繁体   English

SAPUi5单选按钮选中的文本

[英]SAPUi5 Radio Button Selected Text

I have a radio button in my XMLview as shown below. 我的XMLview中有一个单选按钮,如下所示。 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. 我正在尝试在控制器的AfterRendering方法中读取selectedButton文本的值。

XML View: XML视图:

<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. 奇怪的是,无论选择哪个值,我总是得到“ Yes”值。 Can some one advise where I am doing wrong please. 有人可以告诉我我做错了什么。

It does that because the RadioButtonGroup doesn't initially have a property set selectedIndex . 这样做是因为RadioButtonGroup最初没有将属性设置为selectedIndex It seems to fail the getSelectedButton method whenever selectedIndex isn't set. 每当未设置selectedIndex时, getSelectedButton方法似乎都会失败。

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 视图2

<RadioButtonGroup id="rbgtype" selectedIndex ="{= ${rbgtype} ? 0 : 1 }">
            <RadioButton text="Yes" />
            <RadioButton text="No"/>
</RadioButtonGroup>

Please check your xml view radionbuttongroup aggregation and binding 请检查您的xml视图radionbuttongroup聚合和绑定

<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 修复后,您可以在控制器的onInit函数中为onAfterRendering添加事件委托

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 有什么问题让我知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM