cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

If-else statement in XML view

10,359

HI,

I'm a SAPUI5 beginner, having a problem with if-else statement in XML view.

I'm trying to create a table in XML view. Values in table are binded from .json file (in the future, values will be binded via oData service). Below is the code.

<mvc:View
	controllerName="demo.prototype.controller.TestController"
	xmlns:t="sap.ui.table"
	xmlns:mvc="sap.ui.core.mvc"
	xmlns:u="sap.ui.unified"
	xmlns:c="sap.ui.core"
	xmlns="sap.m"
	xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1">
        <t:Table
			rows="{/ProductCollection}"
			visibleRowCount="7"
			selectionMode="MultiToggle"
			width="75rem">
		<t:columns>
			<t:Column width="10rem">
				<Label text="{i18n>modelGroup}" />
	        	<t:template>
					<Label text="{modelGroup}" />
				</t:template>
			</t:Column>
                        <t:Column width="4rem">
				<Label text="{i18n>line}" />
				<t:template>
					<Label text="{line}"/>
				</t:template>
			</t:Column>
                        <t:Column width="6rem">
				<Label text="{i18n>date}" />
				<t:template>
					<Input value="{data1}"
					       description="/100" />
				</t:template>
			</t:Column>
               </t:columns>
	</t:Table>
</mvc:View>

I want to switch Control(Input or Label) or to switch "display description or not" in Input control, depending on flg (true or false) in .json file. But I cannot find the way to do that. I found "http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1" in other questions and tried, but it did not work.

There are 2 ways probably・Implement in Controller ・Implement in XML view.

The 2nd way is preferable for me.

Could anyone help me?

If there is missing information, please point them out.

Thank you for your support.

Accepted Solutions (0)

Answers (2)

Answers (2)

bhuvneshkumar_gupta
Participant

Hi,

I think you are looking for preprocessing concept in XML View -

Have a look on - https://sapui5.hana.ondemand.com/#/topic/fc185952184c48618ef46306a1517f8c

With Preprocessing Instructions you can do stuff like this:

<template:iftest="{marketed}">
 <template:then>
  <Labeltext="product is marketed"/>
 </template:then><template:else>
  <Imagesrc="path.jpg"/>
 </template:else>
</template:if>

If you are looking for simple solution (Not Recommended/Suggetsed approach), you can use expression binding, create two columns(Label and Input) then set the visibility as below

<Input visible= "{= ${condition} === true ? <option1> : <option2>}">

Thank you for your comment. I checked URL and tried, but it does not work.

Do you know whether xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1" is correct or not?

According to message, error is caused by Error: failed to load 'http://schemas/sap/com/sapui5/extension/sap/ui/core/template/1/if.js'

I was looking for alternate but cannot found.

Sorry to bother you again.

0 Likes

As mentioned by the author, I was not able to find the namespace for template. I am surprised that SAP UI5 does not support something as simple as control statements in the XML views out of the box! What is the workaround for this?

Thanks.

junwu
SAP Champion
SAP Champion
0 Likes

you cannot use view with template directly. you have to pre-process the template.

use the easy way : a flag to control the visibility

0 Likes

Thank you for your comment and advice.

I read the document about pre-processing, but it takes a little more time for me to understand that...

I will implement with visible in control for now.