【问题标题】:How to set value in odata of radiobuttons like inputfield如何在输入字段等单选按钮的 odata 中设置值
【发布时间】:2017-03-29 16:27:22
【问题描述】:

我现在正在创建一个表单。在一行中,我只需要回答是或否。代码现在看起来像这样

<Label text="Billable" 
	labelFor="flag"/>
<Input value="{appointments>flag}"
	id="flag"
	placeholder="set a flag in true or false"/>

我需要带有是或否的单选按钮。我试过写单选按钮,但它不起作用

<Label text="Billable" />
<VBox binding="{appointments>flag}">
	<RadioButton  text="true"/>
	<RadioButton  text="false"/>
</VBox>

RadioButton 的 value 属性是不允许的。如何将 radioButton 的值设置为 ODatamodel 中的 {appointments>flag},如上面的输入字段?

【问题讨论】:

  • 恐怕你的问题对我来说并不完全清楚。当您说“不起作用”时,您是什么意思?如果要将 RadioButton selectio 绑定到模型,则属性绑定是 提供的标志是一个布尔值。如果它不是布尔值,则使用格式化函数返回布尔值。

标签: radio-button odata sapui5


【解决方案1】:

如果RadioButtonGroup,您可以使用 selectedIndex 属性从模型中设置单选按钮。

<RadioButtonGroup columns="3" width="500px" selectedIndex="{appointments>flag}">
        <buttons>
            <RadioButton  text="true"/>
            <RadioButton  text="false"/>
        </buttons>
</RadioButtonGroup>

在您的情况下, selectedIndex 应该是 0 或 1,0 为真,1 为假

【讨论】:

  • 你好,谢谢,工作,现在我必须编写一个控制器,如果值为 0,则为是,如果值为 1,则为否。
  • 和另一个问题。我想设置标志,如果为0则标志值为是,如果为1则标志值为否。我该怎么做?
  • 你可以使用双向绑定,它会自动设置到模型中
【解决方案2】:

您应该尝试使用数据将控件与模型绑定。

<RadioButtonGroup columns="3" width="500px" selectedIndex="{/flag}" buttons="{/buttons}"  select="radioSelectionHandler">
            <buttons>
                <RadioButton  text="{label}"/>
            </buttons>
        </RadioButtonGroup>

你的控制器可以是这样的

onInit: function() {
    var radioButtons = {
        "buttons":[
                      {
                          "key" : "true",
                          "label" : "True"
                      },
                      {
                          "key" : "false",
                          "label" : "False"
                      }
                  ] ,

    }       

    this.getView().setModel(new  sap.ui.model.json.JSONModel(radioButtons));
},

radioSelectionHandler : function(event){
    var flag = this.getView().getModel().oData.flag;
    sap.m.MessageToast.show(this.getView().getModel().oData.buttons[flag].key);
}

我添加了您在 if else 签入控制器中使用的 key 属性,而不是使用显示文本。 希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2012-12-17
    • 2011-10-24
    • 2011-05-18
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2018-01-09
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多