Michu93
Michu93

Reputation: 5727

Checking value to set `visible` in SAPUI5

I have to check if the value is null and if it is then I don't have to display label with value. I tried this way:

<f:fields
        visible="{= typeof(${Data>/online/value}) !== 'undefined' 
        &amp;&amp; ${Data>/online/value} !== null }">
  <Text visible="{= ${Data>/name} === 'firstScreen' }" text="{i18n>label}"/>
  <Text visible="{= ${Data>/name} === 'firstScreen' }" text="{=${Data>/online/value}}"/>
</f:fields>

but even if Data>/online/value is null it is displayed. Is there a syntax in visible= or?

Upvotes: 1

Views: 3147

Answers (1)

Jorg
Jorg

Reputation: 7250

To expand on my comment a little, and without knowing what f:fields is exactly, I'm assuming it's one of the aggregations on a control the sap.f namespace, given that it starts with a lower case and an XML namespace of f.

Aggregations are like buckets that hold other elements, and they're part of an actual control. They do not have properties. This can be a little confusing because controls themselves, which can also have other elements, do have properties. This might be a reason the fields are always displayed in the example

It's probably better to move the visible check onto the Text field itself. You can probably shorten the check by coercing the value into a boolean:

!!${Data>/online/value}

When undefined, null or "", it'll evaluate to false, if it's a proper string or number, it'll evaluate to true.

The fields aggregation then looks more like:

<f:fields>
   <Label visible="{= ${Data>/name} === 'firstScreen' &amp;&amp; !!${Data>/online/value} }" text="{i18n>label}"/>
   <Text  visible="{= ${Data>/name} === 'firstScreen' &amp;&amp; !!${Data>/online/value} }" text="{=${Data>/online/value}}"/>
</f:fields>

Upvotes: 2

Related Questions