Samples / Learn / Tree

Tree

Drag and drop of tree nodes: which node was moved where, and what that does to the hierarchy behind it.

Class
z2ui5_cl_smp_app_461
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core, z2ui5.cc
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.StandardTreeItem, sap.m.Tree, sap.ui.core.dnd.DragDropInfo, sap.ui.core.mvc.View, z2ui5.cc.Tree
Documentation
cookbook/model/trees ↗

The class

" @keywords dnd move node hierarchy binding context
" @summary Drag and drop of tree nodes: which node was moved where, and what that does to the hierarchy behind it.
" @docs https://abap2ui5.github.io/docs/cookbook/model/trees
CLASS z2ui5_cl_smp_app_461 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    TYPES:
      BEGIN OF ty_s_child,
        text TYPE string,
      END OF ty_s_child,
      ty_t_child TYPE STANDARD TABLE OF ty_s_child WITH EMPTY KEY,
      BEGIN OF ty_s_root,
        text  TYPE string,
        nodes TYPE ty_t_child,
      END OF ty_s_root.
    DATA t_nodes TYPE STANDARD TABLE OF ty_s_root WITH EMPTY KEY.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.
    METHODS on_event.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_461 IMPLEMENTATION.


  METHOD z2ui5_if_app~main.

    me->client = client.
    IF client->check_on_init( ).
      t_nodes = VALUE #(
          ( text = `Inbox` nodes = VALUE #(
              ( text = `Invoice.pdf` )
              ( text = `Contract.docx` ) ) )
          ( text = `Archive` nodes = VALUE #(
              ( text = `Old_Report.pdf` ) ) )
          ( text = `Trash` nodes = VALUE #( ) ) ).
      view_display( ).
    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( ).
      on_event( ).
    ENDIF.

  ENDMETHOD.


  METHOD on_event.

    IF client->get_event( ) = `MOVE_NODE`.
      " both event args arrive resolved client-side: the binding context
      " paths of the dragged and the drop target item, e.g.
      " /T_NODES/0/NODES/1 (a file) and /T_NODES/2 (a folder) - so parse
      " from the END of the path
      SPLIT client->get_event_arg( ) AT `/` INTO TABLE DATA(lt_drag).
      SPLIT client->get_event_arg( 2 ) AT `/` INTO TABLE DATA(lt_drop).
      DATA(lv_drag_lines) = lines( lt_drag ).
      DATA(lv_drop_lines) = lines( lt_drop ).
      IF lv_drag_lines < 4 OR lv_drop_lines < 2
          OR VALUE #( lt_drag[ lv_drag_lines - 1 ] OPTIONAL ) <> `NODES`
          OR VALUE #( lt_drag[ lv_drag_lines - 3 ] OPTIONAL ) <> `T_NODES`
          OR VALUE #( lt_drop[ lv_drop_lines - 1 ] OPTIONAL ) <> `T_NODES`.
        client->message_toast_display( `drop a file onto a folder` ).
        RETURN.
      ENDIF.
      TRY.
          DATA(lv_from_root)  = CONV i( lt_drag[ lv_drag_lines - 2 ] ) + 1.
          DATA(lv_from_child) = CONV i( lt_drag[ lv_drag_lines ] ) + 1.
          DATA(lv_to_root)    = CONV i( lt_drop[ lv_drop_lines ] ) + 1.
          DATA(ls_child)      = t_nodes[ lv_from_root ]-nodes[ lv_from_child ].
        CATCH cx_root.
          RETURN.
      ENDTRY.
      " dropping a file onto its own parent folder is a no-op
      IF lv_from_root = lv_to_root.
        RETURN.
      ENDIF.
      " IS ASSIGNED, not sy-subrc: a SUCCESSFUL dynamic ASSIGN does not reset
      " sy-subrc on every release (abap2UI5 #1937)
      ASSIGN t_nodes[ lv_from_root ] TO FIELD-SYMBOL(<from>).
      IF <from> IS NOT ASSIGNED.
        RETURN.
      ENDIF.
      ASSIGN t_nodes[ lv_to_root ] TO FIELD-SYMBOL(<to>).
      IF <to> IS NOT ASSIGNED.
        RETURN.
      ENDIF.
      DELETE <from>-nodes INDEX lv_from_child.
      APPEND ls_child TO <to>-nodes.
      " full view rebuild instead of relying on the automatic model push:
      " the z2ui5.cc.Tree companion re-applies the expand state (snapshotted
      " before this roundtrip) only when it renders - a pure model refresh
      " would leave the rebuilt tree binding collapsed
      view_display( ).
    ENDIF.

  ENDMETHOD.


  METHOD view_display.

    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:z2ui5`  v = `z2ui5.cc`
            )->a( n = `xmlns:dnd`    v = `sap.ui.core.dnd` ).

    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Tree - Drag and Drop Nodes`
            )->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 = `Drag a file onto another folder: the drop event ships the binding context ` &&
                   `paths of both tree items, ABAP moves the node inside the nested table and ` &&
                   `redraws the view - the z2ui5.cc.Tree companion keeps the expanded nodes open.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    DATA(tree) = page->ele( `Tree`
        )->a( n = `id`         v = `tree1`
        )->a( n = `items`      v = client->_bind( t_nodes )
        )->a( n = `headerText` v = `Folders` ).

    tree->ele( `dragDropConfig`
        )->ele( n = `DragDropInfo` ns = `dnd`
            )->a( n = `sourceAggregation` v = `items`
            )->a( n = `targetAggregation` v = `items`
            )->a( n = `dropPosition`      v = `On`
            )->a( n = `drop`              v = client->_event(
                                  val   = `MOVE_NODE`
                                  t_arg = VALUE #(
                                      ( `${$parameters>/draggedControl}.getBindingContext().getPath()` )
                                      ( `${$parameters>/droppedControl}.getBindingContext().getPath()` ) ) ) ).

    tree->tag( `StandardTreeItem`
        )->a( n = `title` v = `{TEXT}` ).

    " invisible companion: preserves the tree's expand state across the
    " move roundtrips (snapshot before the request, re-apply after render)
    page->tag( n = `Tree` ns = `z2ui5` ).

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

  ENDMETHOD.
ENDCLASS.