abap2UI5

CSS

Colours single table cells from the backend: the row carries its colour as custom data and a stylesheet turns it into a background.

Z2UI5_CL_SMP_APP_305

Learn CSS UI5 1.71

The facts

Repository
abap2UI5/samples
Class
Z2UI5_CL_SMP_APP_305
Source file
z2ui5_cl_smp_app_305.clas.abap
Category
CSS
Learning path
Reach outside the view
Minimum UI5
1.71 — the floor abap2UI5 holds its samples to
In the playground
runs in the browser, with no system and nothing installed

Keywords: color, background, conditional, formatting, style, data, attribute

Controls it builds

Read out of the builder chain by the abap2UI5 linter, not from the category this sample is filed under — which is what makes “which samples build one of these” a question the catalogue can answer at all.

Libraries

Run it here

The sample running, in this page — the app on its own, with no editor over it. Switch to Playground with this code

Nothing is installed and nothing is sent anywhere: the ABAP is compiled to JavaScript in this browser and runs against abap2UI5 itself.

The ABAP

z2ui5_cl_smp_app_305.clas.abap — 138 lines Read it on GitHub ↗
" @keywords color background conditional formatting style data attribute
" @summary Colours single table cells from the backend: the row carries its colour as custom data and a stylesheet turns it into a background.
CLASS z2ui5_cl_smp_app_305 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    TYPES:
      BEGIN OF ty_s_row,
        title TYPE string,
        value TYPE string,
      END OF ty_s_row.
    DATA t_tab TYPE STANDARD TABLE OF ty_s_row WITH EMPTY KEY.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS set_view.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_305 IMPLEMENTATION.

  METHOD set_view.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc`
            )->a( n = `xmlns:core`   v = `sap.ui.core` ).
    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - CSS - Color Table Cells from the Backend`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `Table cells are coloured from the backend: each cell carries a data-color attribute bound to the ` &&
                   `row, and an inline html style element maps those values to a background colour.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    " raw markup travels in the content attribute of a core:HTML leaf - the
    " builder re-escapes it on stringify, so the literal markup is written here
    page->tag( n = `HTML` ns = `core`
        )->a( n = `content` v = `<style>`
&& `td:has([data-color="red"])\{ `
&& `    background-color: red;`
&& `\}`
&& ``
&& `td:has([data-color="green"])\{`
&& `    background-color: green;`
&& `\}`
&& ``
&& `td:has([data-color="blue"])\{`
&& `    background-color: blue;`
&& `\}`
&& ``
&& `td:has([data-color="orange"])\{`
&& `    background-color: orange;`
&& `\}`
&& ``
&& `td:has([data-color="grey"])\{`
&& `    background-color: grey;`
&& `\}`
&& ``
&& `td:has([data-color="yellow"])\{`
&& `    background-color: yellow;`
&& `\}`
&& `</style>` ).

    DATA(tab) = page->ele( `Table`
        )->a( n = `items` v = client->_bind( t_tab )
        )->a( n = `mode`  v = `MultiSelect`
        )->ele( `headerToolbar`
            )->ele( `OverflowToolbar`
                )->tag( `Title`
                    )->a( n = `text` v = `change cell color`
            )->end(
        )->end( ).

    tab->ele( `columns`
        )->ele( `Column`
            )->tag( `Text`
                )->a( n = `text` v = `Title`
        )->end(
        )->ele( `Column`
            )->tag( `Text`
                )->a( n = `text` v = `Color`
        )->end( ).

    tab->ele( `items`
        )->ele( `ColumnListItem`
            )->ele( `cells`
                )->ele( `Text`
                    )->a( n = `text` v = `{TITLE}`
                    )->ele( `customData`
                        )->tag( n = `CustomData` ns = `core`
                            )->a( n = `value`      v = `{VALUE}`
                            )->a( n = `key`        v = `color`
                            )->a( n = `writeToDom` b = abap_true
                    )->end(
                )->end(
                )->tag( `Input`
                    )->a( n = `enabled` b = abap_true
                    )->a( n = `value`   v = `{VALUE}` ).

    client->view_display( view->stringify( ) ).

  ENDMETHOD.


  METHOD z2ui5_if_app~main.

    me->client = client.

    IF client->check_on_init( ).
      t_tab = VALUE #(
          ( title = `entry 01`  value = `red` )
          ( title = `entry 02`  value = `blue` )
          ( title = `entry 03`  value = `green` )
          ( title = `entry 04`  value = `yellow` )
          ( title = `entry 05`  value = `orange` )
          ( title = `entry 06`  value = `grey` ) ).

      set_view( ).
    ELSEIF client->check_on_navigated( ).
      set_view( ).
    ENDIF.

  ENDMETHOD.

ENDCLASS.

More in CSS

Search every abap2UI5 sample · the full list on one page

On this page