单选按钮组上的SapUi5数据绑定不起作用

时间:2018-08-24 16:18:37

标签: javascript data-binding odata sapui5

请问有人建议使用SAP UI5进行单选按钮的双向数据绑定。在数据模型中,我有一个名为textType的属性,它可以返回“ Y”或“ N”,这取决于我必须选择相应的单选按钮

 <m:RadioButton text="yes" selected="{path:'/textType',formatter: function(val){ if(textType== 'Y') return true; }else{ return false} }"/><m:RadioButton text="No" selected="{Selected}"/>

但是我无法设置该值。我尝试使用单选按钮组,但是没有成功

 <RadioButtonGroup buttons="{/Type}" >
<RadioButton text="YES" selected = "{Selected}" />
<RadioButton text="No" selected = "{Selected}" />
</RadioButtonGroup>

有人可以对此提出建议吗?

更新模型数据代码。

    var stermType = this.getView().byId("rbgTermType").getSelectedButton().getText(),                   
    sElecReg = this.getView().byId("rbgElecReg").getSelectedButton().getText(),                 
    sOpenReg = this.getView().byId("rbgOpenReg").getSelectedButton().getText(),                 
    sConfirmgdpr = this.getView().byId("idConfirmgdpr").getSelected(),                      
    oData = {};                 
    oData = {                   
    Term_Type: stermType,                       
    Electoral_Reg: sElecReg,                    
    Open_Reg: sOpenReg,                     
    Data_Protection_Confirm: sConfirmgdpr,                  
    };

    this.getView().setBusy(true);                   
    var that = this;                    
    var mParams = {                     
    success: function () {                          
    that.getView().setBusy(false);                      
    }.bind(),                   
    error: function (err) {                         
    that.getView().setBusy(false);                             
that.fnShowErrorMessage(jQuery.parseJSON(err.responseText).error.message.value)                     
    }.bind()  
};                      
this.getOwnerComponent().getModel().update(  "/PassportVisaBankCitizenshipDetailsSet(StudentObjid='',PassportNumber='',AccountNumber='')", oData, mParams);
this.getOwnerComponent().getModel().refresh(true);

2 个答案:

答案 0 :(得分:1)

好吧,如果属性textType的值为Y或N,则需要使用表达式绑定:

 <RadioButtonGroup >
   <RadioButton text="YES" selected = "{= ${textType}==='Y'}" />
   <RadioButton text="No"  selected = "{= ${textType}==='N'}" />
 </RadioButtonGroup>

缺点是使用表达式绑定时您会失去双向。因此,您将需要一个onSelect事件处理程序(在api文档中检查事件名称)

如果您的媒体资源是布尔值,那么您可以使用

 <RadioButtonGroup >
   <RadioButton text="YES" selected = "{textType}" />
   <RadioButton text="No"  selected = "{=!${textType}}" />
 </RadioButtonGroup>

完成

更新:这是一个有效的示例:https://jsfiddle.net/iPirat/yhj3eabv/

我使用带有内容的样本模型将单选按钮组上方的位置放到了位

        {
          textType1: 'N',
          textType2: true
        }

第一个{RadioButtonGroup}与{textType1}一起使用,并且事件处理程序是必需的

第二个{RadioButtonGroup}与布尔{textType2}一起使用,不需要事件处理程序。绑定可以为您完成一切。

答案 1 :(得分:1)

您可以执行的最接近的操作将0视为Y,将1视为N。 这是一个有效的例子。 https://jsbin.com/hutuvo/edit?html,output

<!DOCTYPE HTML>
<html>

  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="UTF-8">
    <title>MVC</title>
    <script id="sap-ui-bootstrap" type="text/javascript"
            src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m,sap.ui.table"
            data-sap-ui-xx-bindingSyntax="complex">
    </script>

    <script id="oView" type="sapui5/xmlview">
    <mvc:View
    controllerName="sap.example.Controller"
    xmlns:core="sap.ui.core"
    xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m"
    height="100%">
      <RadioButtonGroup selectedIndex="{/selected}">
        <RadioButton text="YES" />
        <RadioButton text="No" />
      </RadioButtonGroup>
      <Button text="Check" press="checkValue" />
      </mvc:View>
    </script>
    <script>
      sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/ui/model/json/JSONModel"],
                    function(Controller, JSONModel) {
        "use strict";

        var oPageController = Controller.extend("sap.example.Controller", {
          onInit: function() {
            this.getView().setModel(new JSONModel({
              selected: 1
            }));
          },
          checkValue: function() {
            alert(this.getView().getModel().getProperty("/selected") ? "NO" : "YES");
          }
        });
        return oPageController;
      });

      var oView = sap.ui.xmlview({
        viewContent: jQuery('#oView').html()
      });

      oView.placeAt('content');
    </script>
  </head>

  <body class="sapUiBody" role="application">
    <div id="content"></div>
  </body>
</html>