Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding in Control with "class" Attribute

Tags:

css

sapui5

I want to handle the colors of the values in Text control (sap.m). If the value is "TRUE", the color is green. Otherwise it will be red if the value is "FALSE".

<Text
  class="{= ${HintTable>IS_ENABLED} === 'TRUE' ? 'greenTextColor' : redTextColor'}"
  text="{HintTable>IS_ENABLED}"
/>

But it doesn't seem to be working. I mean, the class cannot receive the "greenTextColor" nor "redTextColor".

Did I make something wrong?

like image 669
ThuanNguyen Avatar asked Feb 14 '17 09:02

ThuanNguyen


Video Answer


2 Answers

UI5 doesn't support binding for class in XML view directly as it's not a valid property of ManagedObject. Nevertheless, there is a workaround by adding custom data:

  1. Add CustomData with the property writeToDom to your control. Use your expression binding there:

    <ControlXYZ class="myControl">
      <customData>
        <core:CustomData xmlns:core="sap.ui.core"
          key="green"
          value="foo"
          writeToDom="{= expression}"
        />
      </customData>
    </ControlXYZ>

    Depending on the outcome of your expression binding, data-green will be added to the control's HTML element. The browser can then manipulate the color corresponding to the attribute selector.

  2. Your CSS should thus include the selector accordingly:

    .myApp .sapControlXYZ.myControl[data-green] { /* ... */ }

Here is an example: https://embed.plnkr.co/LAv1qfsUjX0Anu7S/

Of course, you can also bind anything you want to the value property of the CustomData in order to react on more granular CSS selectors. To learn more about how to leverage custom data in DOM, check out the documentation topic Writing Data to the HTML DOM as DATA-* Attribute.


⚠️ Before using custom CSS..

  • There might be controls which don't require custom CSS. Especially when it comes to styling sap.m.Text for example, there is:

    • Text with semantic colors: sap.m.ObjectStatus or .ObjectNumber.
    • Text with custom format: sap.m.FormattedText.
  • SAP explicitly warns not to use custom CSS styles.

    As stated in the Compatibility Rules, the HTML and CSS generated by SAPUI5 is not part of the public API [...]. As such, SAP Fiori launchpad apps should not override styles.

    Generally, the importance of adding custom CSS styles should be always questioned and double-checked with stakeholders to improve UI consistency across Fiori apps as well as to reduce the maintenance costs and TCO that would otherwise rise significantly with custom CSS.

like image 104
Boghyon Hoffmann Avatar answered Sep 21 '22 13:09

Boghyon Hoffmann


The answer of Boghyon Hoffmann is great!

But if none of his options should work for you, you can always resort to using two different copies of the Text element together with the visible attribute (which supports expression binding):

<Text
  class="greenTextColor"
  text="{HintTable>IS_ENABLED}"
  visible="{= ${HintTable>IS_ENABLED === 'TRUE'} }"
/>
<Text
  class="redTextColor"
  text="{HintTable>IS_ENABLED}"
  visible="{= ${HintTable>IS_ENABLED !== 'TRUE'} }"
/>

Tipp:
Simplify your expressions, if your model contains bools instead of strings:

  visible="{= ${HintTable>IS_ENABLED} }"

and

  visible="{= !${HintTable>IS_ENABLED} }"
like image 41
Jpsy Avatar answered Sep 23 '22 13:09

Jpsy