Tree
Drag and drop of tree nodes: which node was moved where, and what that does to the hierarchy behind it.
Z2UI5_CL_SMP_APP_461
Learn
Tree
UI5 1.71
The facts
- Repository
- abap2UI5/samples
- Class
Z2UI5_CL_SMP_APP_461- Source file
z2ui5_cl_smp_app_461.clas.abap↗- Category
- Tree
- Learning path
- Show many rows
- Minimum UI5
- 1.71 — the floor abap2UI5 holds its samples to
- In the playground
- runs in the browser, with no system and nothing installed
- Documentation
- abap2ui5.github.io/docs/cookbook/model/trees ↗
Keywords: dnd, move, node, hierarchy, binding, context
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
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_461.clas.abap — 156 lines
Click a number to link to a line — shift-click for a range.
Read it on GitHub ↗
" @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` ) ).
view_display( ).
ELSEIF client->check_on_navigated( ).
view_display( ).
ELSE.
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.
ASSIGN t_nodes[ lv_from_root ] TO FIELD-SYMBOL(<from>).
IF sy-subrc <> 0.
RETURN.
ENDIF.
ASSIGN t_nodes[ lv_to_root ] TO FIELD-SYMBOL(<to>).
IF sy-subrc <> 0.
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:core` v = `sap.ui.core`
)->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.
More in Tree
On this page