|
| 1 | +--- |
| 2 | +outline: [2, 4] |
| 3 | +--- |
| 4 | +# Nested Views |
| 5 | + |
| 6 | +A nested view is a view that is built from several independent parts. In abap2UI5 there are two main ways to achieve this: composing a single view tree from helper methods, and splitting the app into sub-apps that each own a slice of the view. |
| 7 | + |
| 8 | +#### Composing with Helper Methods |
| 9 | + |
| 10 | +The fluent builder returns each node as an object reference, so you can pass a node into a helper method and let it add children there: |
| 11 | + |
| 12 | +```abap |
| 13 | +METHOD z2ui5_if_app~main. |
| 14 | + IF client->check_on_init( ). |
| 15 | + DATA(xml) = z2ui5_cl_xml_view=>factory( ). |
| 16 | + DATA(page) = xml->shell( )->page( 'My App' ). |
| 17 | +
|
| 18 | + build_header( page ). |
| 19 | + build_content( page ). |
| 20 | + build_footer( page ). |
| 21 | +
|
| 22 | + client->view_display( xml->stringify( ) ). |
| 23 | + ENDIF. |
| 24 | +ENDMETHOD. |
| 25 | +
|
| 26 | +METHOD build_header. |
| 27 | + io_page->toolbar( )->title( 'Header' ). |
| 28 | +ENDMETHOD. |
| 29 | +
|
| 30 | +METHOD build_content. |
| 31 | + io_page->vbox( |
| 32 | + )->text( 'Line 1' |
| 33 | + )->text( 'Line 2' ). |
| 34 | +ENDMETHOD. |
| 35 | +
|
| 36 | +METHOD build_footer. |
| 37 | + io_page->footer( )->button( |
| 38 | + text = 'Save' |
| 39 | + press = client->_event( 'SAVE' ) ). |
| 40 | +ENDMETHOD. |
| 41 | +``` |
| 42 | + |
| 43 | +Each helper receives the parent node by reference and attaches its controls to it. The final `stringify( )` call serialises the entire tree — including all nested children — into one XML string. |
| 44 | + |
| 45 | +#### Sub-Apps |
| 46 | + |
| 47 | +For larger apps it is useful to split responsibility across separate ABAP classes. Each sub-app implements `z2ui5_if_app` and manages its own piece of the view. The parent app instantiates the sub-app, passes the client, and merges the returned view fragment into its own view tree. |
| 48 | + |
| 49 | +**Parent app:** |
| 50 | +```abap |
| 51 | +METHOD z2ui5_if_app~main. |
| 52 | + IF client->check_on_init( ). |
| 53 | + sub_app = NEW z2ui5_cl_my_sub_app( ). |
| 54 | + ENDIF. |
| 55 | +
|
| 56 | + DATA(xml) = z2ui5_cl_xml_view=>factory( ). |
| 57 | + DATA(page) = xml->shell( )->page( 'Parent' ). |
| 58 | +
|
| 59 | + " Let the sub-app render into a container on the page |
| 60 | + DATA(container) = page->vbox( ). |
| 61 | + sub_app->render( client = client io_node = container ). |
| 62 | +
|
| 63 | + client->view_display( xml->stringify( ) ). |
| 64 | +ENDMETHOD. |
| 65 | +``` |
| 66 | + |
| 67 | +**Sub-app:** |
| 68 | +```abap |
| 69 | +METHOD render. |
| 70 | + io_node->text( 'I am the sub-app' ). |
| 71 | + io_node->button( |
| 72 | + text = 'Sub Action' |
| 73 | + press = io_client->_event( 'SUB_ACTION' ) ). |
| 74 | +ENDMETHOD. |
| 75 | +``` |
| 76 | + |
| 77 | +The sub-app appends its controls to whatever node the parent provides. Events fired inside the sub-app are handled by the sub-app's own `main` method on the next roundtrip. |
| 78 | + |
| 79 | +#### Navigation Between Views |
| 80 | + |
| 81 | +When the nested content changes completely — for example, a detail page replacing a list — use `view_display` to swap the entire view rather than nesting: |
| 82 | + |
| 83 | +```abap |
| 84 | +CASE client->get_event( ). |
| 85 | + WHEN 'OPEN_DETAIL'. |
| 86 | + client->view_display( detail_view->stringify( ) ). |
| 87 | + WHEN 'BACK'. |
| 88 | + client->view_display( list_view->stringify( ) ). |
| 89 | +ENDCASE. |
| 90 | +``` |
| 91 | + |
| 92 | +Reserve true nesting for content that appears side-by-side or in a stable layout shell. Use `view_display` for sequential navigation where one screen fully replaces another. |
| 93 | + |
| 94 | +#### Tips |
| 95 | + |
| 96 | +- Keep each helper or sub-app focused on one logical section. A helper that grows beyond ~30 lines is a candidate for its own sub-app class. |
| 97 | +- Sub-apps can hold their own instance attributes for local state. Because the whole app tree is reconstructed on every roundtrip, each sub-app re-renders from its current state automatically. |
| 98 | +- Pass only the node the sub-app needs, not the whole view root. This limits the sub-app's scope and makes the boundary explicit. |
| 99 | + |
| 100 | +See `Z2UI5_CL_DEMO_APP_160` for an end-to-end example of parent and sub-app composition. |
0 commit comments