首页 > 解决方案 > SAP UI5 XML 视图中的控制语句 if-else

问题描述

我对 UI5 技术有疑问。

在 XML View 中我们如何使用控制语句?例如像 if-else。我有一个条件,如果满足,应该呈现一些不同的代码行。我查了一下,我可以找到预处理器指令:

https://help.sap.com/viewer/40826922922346f890185c8ff02d30da/1.17/en-US/fc185952184c48618ef46306a1517f8c.html

代码:

<template:if test="{meta>ImageUrl}">
  <template:then>
    <Image src="{path: 'meta>ImageUrl', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}" />
  </template:then>
  <template:else>
    <Text text="{path: 'meta>Title/Value', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}" />
  </template:else>
</template:if>

但是,“模板”的命名空间 (xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1") 不再存在。

有谁知道如何让这个工作?我不想使用三元运算符,这对于这种情况是不可行的。

标签: xmlsapui5

解决方案


您可以使用可见属性。然后控件将被渲染但不显示。格式化程序使条件无论是undefined还是null都更易于阅读。

XML:

<Image visible="{ path: 'meta>ImageUrl', formatter: '.Formatter.visible' }" src="{path: 'meta>ImageUrl', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}" />
<Text visible="{ path: 'meta>ImageUrl', formatter: '.Formatter.visible' }" text="{path: 'meta>Title/Value', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}" />

格式化程序:

visible : function(value) {
  return !(typeof(value) === 'undefined' || value === null);
}

推荐阅读