abap2UI5

OData Annotations TreeBinding

Illustrates how to bind to data from an OData model using $metadata annotations.

Z2UI5_CL_SMPC_APP_366

Controls sap.ui.table UI5 1.71

The facts

Repository
abap2UI5/samples-controls
Class
Z2UI5_CL_SMPC_APP_366
Source file
z2ui5_cl_smpc_app_366.clas.abap
Library
sap.ui.table
UI5 entity
sap.ui.table.TreeTable
Demo kit sample
sap.ui.table.sample.TreeTable.ODataAnnotationsTreeBinding
SAP's own sample
running at sdk.openui5.org ↗
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: treetable, tree, table, sap.ui.table, treetable.odataannotationstreebinding, column, label, text

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_smpc_app_366.clas.abap — 187 lines Read it on GitHub ↗
" @keywords treetable tree table sap.ui.table treetable.odataannotationstreebinding column label text
" @summary Illustrates how to bind to data from an OData model using $metadata annotations.
CLASS z2ui5_cl_smpc_app_366 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    " the mock service's Nodes are a FLAT list carrying HierarchyLevel /
    " NodeID / ParentNodeID / DrillState, which only an OData tree binding can
    " assemble into a tree. abap2UI5 serves one JSON model, so the same nodes
    " are modelled NESTED here (one children table per level) and bound with
    " arrayNames - the JSON tree binding the framework does support. Every
    " node keeps all FIVE of its own fields (NodeID, HierarchyLevel, Description,
    " ParentNodeID, DrillState); four of them are rendered as columns, and the
    " fifth - ParentNodeID - is what the nesting replaces, carried but not shown
    TYPES:
      BEGIN OF ty_s_node3,
        nodeid         TYPE i,
        hierarchylevel TYPE i,
        description    TYPE string,
        parentnodeid   TYPE string,
        drillstate     TYPE string,
      END OF ty_s_node3,
      ty_t_node3 TYPE STANDARD TABLE OF ty_s_node3 WITH EMPTY KEY,
      BEGIN OF ty_s_node2,
        nodeid         TYPE i,
        hierarchylevel TYPE i,
        description    TYPE string,
        parentnodeid   TYPE string,
        drillstate     TYPE string,
        children       TYPE ty_t_node3,
      END OF ty_s_node2,
      ty_t_node2 TYPE STANDARD TABLE OF ty_s_node2 WITH EMPTY KEY,
      BEGIN OF ty_s_node1,
        nodeid         TYPE i,
        hierarchylevel TYPE i,
        description    TYPE string,
        parentnodeid   TYPE string,
        drillstate     TYPE string,
        children       TYPE ty_t_node2,
      END OF ty_s_node1,
      ty_t_node1 TYPE STANDARD TABLE OF ty_s_node1 WITH EMPTY KEY,
      BEGIN OF ty_s_node0,
        nodeid         TYPE i,
        hierarchylevel TYPE i,
        description    TYPE string,
        parentnodeid   TYPE string,
        drillstate     TYPE string,
        children       TYPE ty_t_node1,
      END OF ty_s_node0.
    DATA t_nodes TYPE STANDARD TABLE OF ty_s_node0 WITH EMPTY KEY.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.
    METHODS model_init.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smpc_app_366 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client = client.
    IF client->check_on_init( ).
      model_init( ).
      view_display( ).
    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ENDIF.

  ENDMETHOD.


  METHOD view_display.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory( ).

    " the tree comes from the model's own nesting instead of the OData tree
    " ANNOTATIONS the service metadata carries; numberOfExpandedLevels is kept
    " as a binding parameter, so the first level opens like in the original.
    view->ele( n = `View` ns = `mvc`
        )->a( n = `xmlns`     v = `sap.ui.table`
        )->a( n = `xmlns:m`   v = `sap.m`
        )->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`

        )->ele( `TreeTable`
            )->a( n = `id`                     v = `treeTable`
            )->a( n = `selectionMode`          v = `Single`
            )->a( n = `enableColumnReordering` v = `false`
            )->a( n = `rows`                   v = |\{ path: '{ client->_bind_path( t_nodes ) }', parameters: \{ arrayNames: ['CHILDREN'], numberOfExpandedLevels: 1 \} \}|

            )->ele( `columns`
                )->ele( `Column`
                    )->tag( n = `Label` ns = `m`
                        )->a( n = `text` v = `Description`

                    )->ele( `template`
                        )->tag( n = `Text` ns = `m`
                            )->a( n = `text`     v = `{DESCRIPTION}`
                            )->a( n = `wrapping` v = `false`

                    )->end(
                )->end(
                )->ele( `Column`
                    )->tag( n = `Label` ns = `m`
                        )->a( n = `text` v = `HierarchyLevel`

                    )->ele( `template`
                        )->tag( n = `Text` ns = `m`
                            )->a( n = `text`     v = `{HIERARCHYLEVEL}`
                            )->a( n = `wrapping` v = `false`

                    )->end(
                )->end(
                )->ele( `Column`
                    )->tag( n = `Label` ns = `m`
                        )->a( n = `text` v = `NodeID`

                    )->ele( `template`
                        )->tag( n = `Text` ns = `m`
                            )->a( n = `text`     v = `{NODEID}`
                            )->a( n = `wrapping` v = `false`

                    )->end(
                )->end(
                )->ele( `Column`
                    )->tag( n = `Label` ns = `m`
                        )->a( n = `text` v = `ParentNodeID`

                    )->ele( `template`
                        )->tag( n = `Text` ns = `m`
                            )->a( n = `text`     v = `{PARENTNODEID}`
                            )->a( n = `wrapping` v = `false`

                    )->end(
                )->end(
            )->end( ).

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

  ENDMETHOD.


  METHOD model_init.

    " localService/mockdata/Nodes.json - the sixteen nodes of the mock service,
    " re-shaped from the flat parent/child list into the nesting the JSON tree
    " binding needs. Node ids, levels, descriptions, parent ids and drill states
    " are the mock's own values
    t_nodes = VALUE #(
      ( nodeid = 1 hierarchylevel = 0 description = `1` parentnodeid = `` drillstate = `expanded`
        children = VALUE #(
          ( nodeid = 4 hierarchylevel = 1 description = `1.1` parentnodeid = `1` drillstate = `leaf` )
          ( nodeid = 5 hierarchylevel = 1 description = `1.2` parentnodeid = `1` drillstate = `expanded`
            children = VALUE #(
              ( nodeid = 6 hierarchylevel = 2 description = `1.2.1` parentnodeid = `5` drillstate = `leaf` )
              ( nodeid = 7 hierarchylevel = 2 description = `1.2.2` parentnodeid = `5` drillstate = `leaf` )
            ) )
        ) )
      ( nodeid = 2 hierarchylevel = 0 description = `2` parentnodeid = `` drillstate = `expanded`
        children = VALUE #(
          ( nodeid = 8  hierarchylevel = 1 description = `2.1` parentnodeid = `2` drillstate = `leaf` )
          ( nodeid = 9  hierarchylevel = 1 description = `2.2` parentnodeid = `2` drillstate = `leaf` )
          ( nodeid = 10 hierarchylevel = 1 description = `2.3` parentnodeid = `2` drillstate = `leaf` )
        ) )
      ( nodeid = 3 hierarchylevel = 0 description = `3` parentnodeid = `` drillstate = `expanded`
        children = VALUE #(
          ( nodeid = 11 hierarchylevel = 1 description = `3.1` parentnodeid = `3` drillstate = `expanded`
            children = VALUE #(
              ( nodeid = 12 hierarchylevel = 2 description = `3.1.1` parentnodeid = `11` drillstate = `expanded`
                children = VALUE #(
                  ( nodeid = 13 hierarchylevel = 3 description = `3.1.1.1` parentnodeid = `12` drillstate = `leaf` )
                  ( nodeid = 14 hierarchylevel = 3 description = `3.1.1.2` parentnodeid = `12` drillstate = `leaf` )
                  ( nodeid = 15 hierarchylevel = 3 description = `3.1.1.3` parentnodeid = `12` drillstate = `leaf` )
                  ( nodeid = 16 hierarchylevel = 3 description = `3.1.1.4` parentnodeid = `12` drillstate = `leaf` )
                ) )
            ) )
        ) )
      ).

  ENDMETHOD.

ENDCLASS.

More in sap.ui.table

Search every abap2UI5 sample · the full list on one page

On this page