【问题标题】:SapUi5 data binding on the radio button group is not working单选按钮组上的 SapUi5 数据绑定不起作用
【发布时间】:2019-01-31 03:54:56
【问题描述】:

有人可以就单选按钮与 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);

【问题讨论】:

  • 格式化程序和表达式绑定破坏双向数据绑定。也许您需要重新设计您的 odata 后端。而不是 Y 或 N 返回布尔值。

标签: javascript data-binding odata sapui5


【解决方案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>

【讨论】:

    【解决方案2】:

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

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

    缺点是在使用表达式绑定时会丢失双向。所以你需要一个 onSelect 事件处理程序(检查 api 文档中的事件名称)

    如果您的属性是 Boolean,那么您可以使用

     <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} 并且不需要事件处理程序。绑定为您完成一切。

    【讨论】:

    • 感谢它的工作。我还没有尝试过两种方式绑定,将我的数据类型更改为布尔值并将尝试保存数据并检查..
    • 谢谢,该代码不适用于双向绑定。还有其他建议吗?我正在使用上面的代码行在我的保存按钮的按下事件上进行两种方式的绑定。用代码编辑了我的问题..
    • 我添加了一个 jsfiddle 示例来演示何时需要事件处理程序,以及何时双向绑定在没有任何事件处理程序的情况下工作
    猜你喜欢
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2015-07-30
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多