THI
THI

Reputation: 365

If - else condition in XML View UI5 application

In my XML view for one of the fields I want to display "Yes" if value from Model parts is "S" or "P" and for rest of all the values want to display "No".

text="{= ${order>/parts} === 'S' ? "Yes" : ${order>/parts} === 'P' ? "Yes" : "No} }"/>

Also, how to write - if ${order>/parts} has "S" AND ${order>/stock} has "A" then display Yes else No in the similar notion like above?

Upvotes: 0

Views: 6341

Answers (4)

Glauco de Souza
Glauco de Souza

Reputation: 29

It helped me too.

In my case it was using icons:

<Button icon="{= ${Approved} === true ? 'sap-icon://accept' : 'sap-icon://approve'}" press="onApproveProduct" />

Upvotes: 0

Matthijs Mennens
Matthijs Mennens

Reputation: 1145

I tried this small example and it works just fine. Maybe because you use double quotes in both opening the text property and at the values ("Yes" and "No"). Try replacing the " with ' in your values. If that doesn't work, you should check if {order>/parts} is not undefined.

View

<Input value="{= ${test1} === 'S' ? 'Yes' : ${test1} === 'P' ? 'Yes' : 'No'}" />
<Input value="{= ${test1} === 'S' ? ${test2} === 'P' ? 'Yes' : 'No' : 'No' }" />

or

<Input value="{= ${test1} === 'S' || ${test1} === 'P' ? 'Yes' : 'No'}" />
<Input value="{= ${test1} === 'S'  &amp;&amp; ${test2} === 'P' ? 'Yes' : 'No'}"/>

Controller

    onInit: function() {
        var oModel = new sap.ui.model.json.JSONModel({
            test1: "S",
            test2: "P"
        });

        var bindingContext = new sap.ui.model.Context();
        bindingContext.oModel = oModel;
        bindingContext.sPath = "/";

        this.getView().setBindingContext(bindingContext);
        this.getView().setModel(oModel);
    }

Upvotes: 2

Inizio
Inizio

Reputation: 2256

Hope this will help you

<Input value="{= (${order>/parts} === 'S' || ${order>/parts} === 'P') ? 'Yes' : 'No' }" />
<Input value="{= (${order>/parts} === 'S' &amp;&amp; ${order>/stock} === 'A') ? 'Yes' : 'No' }" />

Upvotes: 1

piotr-koca
piotr-koca

Reputation: 94

You should read about formatters, which are exactly what you require

Upvotes: 3

Related Questions