如何在SAPUI5 XML-View中使用if-else条件? [英] How to use an if-else condition in a SAPUI5 XML-View?

查看:411
本文介绍了如何在SAPUI5 XML-View中使用if-else条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SAPUI5的XML-View中实现if-else条件,该条件使用来自 JSONModel 的标志(条件)?

How can I implement an if-else condition in a XML-View in SAPUI5 that uses a flag (condition) from a JSONModel?

到目前为止,我有一个控制器

So far I have a Controller:

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

    return Controller.extend("sap.ui.demo.myApp.myController", {
        onInit: function () {
            //// set data model on view
            var oData = {
                title: "A cool title",
                values: [{name: "Text 1", marketed: true}, {name: "Text 2", marketed: false}, {name: "Text 3", , marketed: true}]
            };

            var oModel = new JSONModel(oData);
            this.getView().setModel(oModel);
        }
    });
});

查看

<mvc:View
    controllerName="sap.ui.demo.myApp.myController"
    xmlns="sap.m">

    <!-- using aggregation binding -->
    <Panel expandable="true" expanded="true" headerText="{/title}" width="100%" content="{/values}">
        <content>
            <Label text="{name}"/>
            <!-- if {marketed} 
                    <Label text="product is marketed"/> 
                 else 
                    add nothing
            -->
        </content>
    </Panel>

</mvc:View>

修改

有没有更好的方法来实现它而不是实现一种矫枉过正的XML-Preprocessor?

Is there a better way to do it than by implementing an overkill-feeling XML-Preprocessor?

推荐答案

OpenUI5支持< a href =https://sapui5.hana.ondemand.com/#/topic/c27d49caa48e424eb75391ae85da2134\"rel =nofollow noreferrer>预处理说明和表达式绑定

OpenUI5 supports Preprocessing Instructions and Expression Binding.

使用预处理指令,你可以做这样的事情:

With Preprocessing Instructions you can do stuff like this:

<template:if test="{marketed}">
    <template:then>
        <Label text="product is marketed" />
    </template:then>
    <template:else>
        <Image src="path.jpg" />
    </template:else>
</template:if>

我不确定测试是否在第一行测试 null / not null true / false 。这是表达式绑定可能很方便的地方:它允许在大括号内的复杂表达式:

I am not sure if the test in the first line tests for null/not null or true/false. This is where the Expression Binding might be handy: it allows for complex expressions within the curly brackets:

<template:if test="{= ${marketed} === true}">






编辑

以下解决方案可能更简单,但似乎有点hacky。

The following solution might be more simple, but seems a little hacky.

在XML视图中嵌入这两个元素,但是使用复杂的表达式绑定来切换可见性:

Embed both elements in your XML view, but toggle the visibility with complex expression binding:

<Label text="product is marketed" visible="{= ${marketed} === true}"/>
<Image src="path.jpg" visible="{= ${marketed} === false}"/>

这篇关于如何在SAPUI5 XML-View中使用if-else条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆