首页 > 解决方案 > 根据 XML 视图中的条件绑定数据。SAPUI5

问题描述

我已经定义了图标选项卡。我必须根据条件在选项卡的一个字段中显示数据。例如:

对于下面的列,我必须根据条件显示数据。

if text.type ===1, 
     display text.field1 
else if text.type ===2,
     .... text.field2
else
     .... text.field3
endif. 

代码片段:

<ColumnListItem type="Active">
<cells>
<Text text="{= ${/TxtModel/AssignType} === '1' ?  }" width="auto" maxLines="2" wrapping="false" textAlign="Center" textDirection="Inherit"/>

</ColumnListItem>

标签: javascriptxmlsapui5

解决方案


利用预处理指令

我正在使用您有 4 个基于AssignType员工为 1 或 5 或 8 的用例。这里我们正在使用if/then/else指令。

  <template:if test="{= {${aufnrTxtModel>/Ltxt/AssignType} === 1}">
      <template:then>
         <Text text="{aufnrTxtModel>/Ltxt/Pernr}" width="auto" maxLines="2" wrapping="false" textAlign="Center" textDirection="Inherit"/>
      </template:then>
      <template:elseif test="{= {${aufnrTxtModel>/Ltxt/AssignType} === 5}">
        <Text text="{aufnrTxtModel>/Ltxt/Ingpr}" width="auto" maxLines="2" wrapping="false" textAlign="Center" textDirection="Inherit"/>
      </template:elseif>
    <template:elseif test="{= {${aufnrTxtModel>/Ltxt/AssignType} === 8}">
        <Text text="{aufnrTxtModel>/WCPL}" width="auto" maxLines="2" wrapping="false" textAlign="Center" textDirection="Inherit"/>
      </template:elseif>
      <template:else>
        <Text text="" width="auto" maxLines="2" wrapping="false" textAlign="Center" textDirection="Inherit"/>
      </template:else>
    </template:if>

您还可以使用自定义格式化程序,您可以在路径中传递AssignType, Pernr, Ingpr,WCPL这些将在您的formatter函数中可用,您可以在其中编写所有逻辑,这比这个复杂的 xml 更容易更改。


推荐阅读