简体   繁体   English

如何在 SAPUI5 XML 视图中使用 if-else 条件?

[英]How to use an if-else condition in a SAPUI5 XML-View?

How can I implement an if-else condition in a XML-View in SAPUI5 that uses a flag (condition) from a JSONModel ?如何在使用来自 JSONModel 的标志(条件)的JSONModel中的 XML 视图中实现 if-else 条件?

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);
        }
    });
});

and a View :和一个视图

<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>

Edit :编辑

Is there a better way to do it than by implementing an overkill-feeling XML-Preprocessor?有没有比实施感觉矫枉过正的 XML 预处理器更好的方法呢?

OpenUI5 supports Preprocessing Instructions and Expression Binding . OpenUI5支持预处理指令表达式绑定

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>

I am not sure if the test in the first line tests for null/not null or true/false . 我不知道,如果test在第一线测试null/not nulltrue/false This is where the Expression Binding might be handy: it allows for complex expressions within the curly brackets: 这是表达式绑定可能很方便的地方:它允许在大括号内的复杂表达式:

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

Edit 编辑

The following solution might be more simple, but seems a little hacky. 以下解决方案可能更简单,但似乎有点hacky。

Embed both elements in your XML view, but toggle the visibility with complex expression binding: 将这两个元素嵌入XML视图中,但使用复杂的表达式绑定切换可见性:

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

I am not sure I got your requirement, but it looks like simply binding the visible attribute to the marketed-flag would do. 我不确定我是否满足您的要求,但看起来只是简单地将visible属性绑定到marketke-flag就可以了。

If you need to bind in a negated way you can use expression binding like 如果需要以否定方式绑定,可以使用表达式绑定

 visible="{= !${/marketed}}"

If I've understood your question propery, you could use a formatter function. 如果我理解你的问题,你可以使用格式化函数。

<Label text="{
    path: 'marketed'
    formatter: '.formatter.marketed'
}"/>

.formatter.marketed in this example references a function in a separate formatter.js file: 此示例中的.formatter.marketed引用了一个单独的formatter.js文件中的函数:

marketed: function(marketed) {
    if(marketed) {
        return 'product is marketed';
    } else {
        return '';
    }
}

See the ui5 SDK for hpow to set up the formatter function correctly: 有关hpow的信息,请参阅ui5 SDK以正确设置formatter函数:

https://sapui5.hana.ondemand.com/sdk#docs/guide/0f8626ed7b7542ffaa44601828db20de.html https://sapui5.hana.ondemand.com/sdk#docs/guide/0f8626ed7b7542ffaa44601828db20de.html

In your example, as this is just a label, we're returning an empty string, so it will just be blank. 在你的例子中,因为这只是一个标签,我们返回一个空字符串,所以它只是空白。 The Label will still be rendered, but it's an empty string, so there's nothing to show, so the user will never know it's there. Label仍然会被渲染,但它是一个空字符串,所以没有什么可以显示,所以用户永远不会知道它在那里。 I'm not entirely sure, but if you return undefined instead of an empty string, the label may not be rendered at all. 我不完全确定,但如果你返回undefined而不是空字符串,标签可能根本不会被渲染。

In the context of XML Templating as one of the Preprocessing Instructions, another option is to define Fragment's fragmentName as an Expression Binding like this:在作为预处理指令之一的XML 模板的上下文中,另一种选择是将 Fragment 的fragmentName定义为表达式绑定,如下所示:

<core:Fragment fragmentName="{= ${path: 'facet>Target', formatter: 'sap.ui.model.odata.AnnotationHelper.isMultiple'} === 'true'
    ? 'sap.ui.core.sample.ViewTemplate.scenario.TableFacet'
    : 'sap.ui.core.sample.ViewTemplate.scenario.FormFacet' }" type="XML"/>

Documentation: https://ui5.sap.com/#/topic/65da02badf704e03a4fd6bd4c5aba8f4文档: https ://ui5.sap.com/#/topic/65da02badf704e03a4fd6bd4c5aba8f4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM