如何在text属性中使用if else来显示一个值或另一个值?

时间:2018-06-08 21:32:34

标签: sapui5

有两个不同的值,例如QtValidFDocDate,我希望只有在DocDate到来时才能显示QtValidF null < / strong>即可。我怎么能这样做呢?

<Text text="{
  path: 'QtValidF',
  formatter:'.formatoFecha'
}"/>

我试过这个但是没有用。

<Text text="{= ${QtValidF} != 'null' ? {
  path: 'DocDate',
  formatter: '.formatoFecha'
} : {
  path: 'QtValidF',
  formatter: '.formatoFecha'
}}"/>

2 个答案:

答案 0 :(得分:0)

以下是inline binding expression

value='{= ${/QValidF} !== null ? ${/QValidF} : ${/DocDate} }'

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>Bernard LeTourneur - UI5 Single Page</title>
        <script
        	src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js'
            id='sap-ui-bootstrap'
            data-sap-ui-theme='sap_bluecrystal'
            data-sap-ui-libs='sap.m'
            data-sap-ui-bindingSyntax='complex'
            data-sap-ui-compatVersion='edge'
            data-sap-ui-preload='async'>    
        </script>
        <script id='myXmlView' type='ui5/xmlview'>
            <mvc:View
                controllerName='MyController'
                xmlns='sap.m'
                xmlns:core='sap.ui.core'
                xmlns:mvc='sap.ui.core.mvc'>
                <Input id='inputStatus' enabled='false' value='{= ${/QValidF} !== null ? ${/QValidF} : ${/DocDate} }' />
                <Button press="handleDataChange" text='{= ${/QValidF} !== null ? "Set QValidF to Null" : "Set QValidF to something"}' />
            </mvc:View>
        </script>
        <script>
            sap.ui.getCore().attachInit(function () {
                'use strict';

                sap.ui.define([
                    'sap/ui/core/mvc/Controller',
                    'sap/ui/model/json/JSONModel'
                ], function (Controller, JSONModel) {
                    'use strict';

                    return Controller.extend('MyController', {

                        onInit : function () {                     
                            var that = this;
                            var data = {QValidF: "something", DocDate: "2018/06/08"};
                            var oModel = new JSONModel(data);
                            this.getView().setModel(oModel);
                        },
                        handleDataChange: function(){
                            var isNull = this.getView().getModel().getProperty("/QValidF");
                            console.log(isNull);
                            if (isNull=="something"){
                                this.getView().getModel().setProperty("/", {QValidF: null, DocDate: "2018/06/08"});
                            }
                            else{
                                this.getView().getModel().setProperty("/", {QValidF: "something", DocDate: "2018/06/08"});
                            }
                        }
                    });
                });

                sap.ui.xmlview({
                    viewContent : jQuery('#myXmlView').html()
                }).placeAt('content');

            });
        </script>
    </head>
    <body class='sapUiBody'>
        <div id='content'></div>
    </body>
</html>

答案 1 :(得分:0)

表达式绑定

通常,您可以将expression binding用于简短的条件表达式。

<MyControl anyProperty="{= ${myPrimary} || ${mySecondary}}" />

就像在JS中一样,当第一个操作数产生falsy values之一时,第二个操作数会被考虑在内。

格式化

如果是formatter is preferred,可以使用复合绑定语法:

<MyControl anyProperty="{
  parts: [
    'myPrimary',
    'mySecondary'
  ],
  formatter: '.getThatValue'
}" />

然后在控制器中:

getThatValue: function(primaryValue, secondaryValue) {
  return primaryValue || secondaryValue;
},

有关详细信息,请查看主题Binding SyntaxComposite Binding