|
| 1 | +# Tables, Trees |
| 2 | +In this section, we will explore how to bind deep data models, such as tables and trees. |
| 3 | + |
| 4 | +### Tables |
| 5 | +The example below demonstrates how to bind a simple table to a UI5 control: |
| 6 | +```abap |
| 7 | +CLASS z2ui5_cl_sample_tab DEFINITION PUBLIC. |
| 8 | + |
| 9 | + PUBLIC SECTION. |
| 10 | + INTERFACES z2ui5_if_app. |
| 11 | +
|
| 12 | + TYPES: |
| 13 | + BEGIN OF ty_row, |
| 14 | + count TYPE i, |
| 15 | + value TYPE string, |
| 16 | + descr TYPE string, |
| 17 | + END OF ty_row. |
| 18 | + DATA t_tab TYPE STANDARD TABLE OF ty_row WITH EMPTY KEY. |
| 19 | +
|
| 20 | +ENDCLASS. |
| 21 | + |
| 22 | +CLASS z2ui5_cl_sample_tab IMPLEMENTATION. |
| 23 | + METHOD z2ui5_if_app~main. |
| 24 | + |
| 25 | + DO 100 TIMES. |
| 26 | + DATA ls_row TYPE ty_row. |
| 27 | + ls_row-count = sy-index. |
| 28 | + ls_row-value = 'red'. |
| 29 | + ls_row-descr = 'this is a description'. |
| 30 | + INSERT ls_row INTO TABLE t_tab. |
| 31 | + ENDDO. |
| 32 | + |
| 33 | + DATA(view) = z2ui5_cl_xml_view=>factory( )->page( )->table( |
| 34 | + items = client->_bind( t_tab ) ). |
| 35 | + |
| 36 | + tab->columns( |
| 37 | + )->column( )->text( 'Color' )->get_parent( |
| 38 | + )->column( )->text( 'Info' )->get_parent( |
| 39 | + )->column( )->text( 'Description' ). |
| 40 | + tab->items( )->column_list_item( )->cells( |
| 41 | + )->text( '{VALUE}' |
| 42 | + )->text( '{INFO}' |
| 43 | + )->text( '{DESCR}' ). |
| 44 | + |
| 45 | + client->view_display( view->stringify( ) ). |
| 46 | + |
| 47 | + ENDMETHOD. |
| 48 | +ENDCLASS. |
| 49 | +
|
| 50 | +``` |
| 51 | + |
| 52 | +### Editable |
| 53 | +Making a table editable is a simple change. You just need to switch the binding mode to `bind_edit` : |
| 54 | +```abap |
| 55 | + METHOD z2ui5_if_app~main. |
| 56 | + |
| 57 | + DO 100 TIMES. |
| 58 | + DATA ls_row TYPE ty_row. |
| 59 | + ls_row-count = sy-index. |
| 60 | + ls_row-value = 'red'. |
| 61 | + ls_row-descr = 'this is a description'. |
| 62 | + INSERT ls_row INTO TABLE t_tab. |
| 63 | + ENDDO. |
| 64 | + |
| 65 | + DATA(view) = z2ui5_cl_xml_view=>factory( )->page( )->table( |
| 66 | + items = client->_bind_edit( t_tab ) ). |
| 67 | + |
| 68 | + tab->columns( |
| 69 | + )->column( )->text( 'Color' )->get_parent( |
| 70 | + )->column( )->text( 'Info' )->get_parent( |
| 71 | + )->column( )->text( 'Description' ). |
| 72 | + tab->items( )->column_list_item( )->cells( |
| 73 | + )->text( '{VALUE}' |
| 74 | + )->text( '{INFO}' |
| 75 | + )->text( '{DESCR}' ). |
| 76 | + |
| 77 | + client->view_display( view->stringify( ) ). |
| 78 | + |
| 79 | + ENDMETHOD. |
| 80 | +``` |
| 81 | + |
| 82 | +### Tree |
| 83 | +To work with trees, you need to use nested structures. Here is an example: |
| 84 | +```abap |
| 85 | +CLASS z2ui5_cl_sample_tree DEFINITION |
| 86 | + PUBLIC |
| 87 | + FINAL |
| 88 | + CREATE PUBLIC . |
| 89 | + |
| 90 | + PUBLIC SECTION. |
| 91 | + INTERFACES z2ui5_if_app. |
| 92 | + |
| 93 | + TYPES: |
| 94 | + BEGIN OF ty_prodh_node_level3, |
| 95 | + is_selected TYPE abap_bool, |
| 96 | + text TYPE string, |
| 97 | + prodh TYPE string, |
| 98 | + END OF ty_prodh_node_level3, |
| 99 | + BEGIN OF ty_prodh_node_level2, |
| 100 | + is_selected TYPE abap_bool, |
| 101 | + text TYPE string, |
| 102 | + prodh TYPE string, |
| 103 | + nodes TYPE STANDARD TABLE OF ty_prodh_node_level3 WITH DEFAULT KEY, |
| 104 | + END OF ty_prodh_node_level2, |
| 105 | + BEGIN OF ty_prodh_node_level1, |
| 106 | + is_selected TYPE abap_bool, |
| 107 | + text TYPE string, |
| 108 | + prodh TYPE string, |
| 109 | + nodes TYPE STANDARD TABLE OF ty_prodh_node_level2 WITH DEFAULT KEY, |
| 110 | + END OF ty_prodh_node_level1, |
| 111 | + ty_prodh_nodes TYPE STANDARD TABLE OF ty_prodh_node_level1 WITH DEFAULT KEY. |
| 112 | + |
| 113 | + DATA prodh_nodes TYPE ty_prodh_nodes. |
| 114 | +
|
| 115 | +ENDCLASS. |
| 116 | +
|
| 117 | +CLASS z2ui5_cl_sample_tree IMPLEMENTATION. |
| 118 | + METHOD z2ui5_if_app~main. |
| 119 | + |
| 120 | + prodh_nodes = |
| 121 | + VALUE #( ( text = 'Machines' |
| 122 | + prodh = '00100' |
| 123 | + nodes = VALUE #( ( text = 'Pumps' |
| 124 | + prodh = '0010000100' |
| 125 | + nodes = VALUE #( ( text = 'Pump 001' |
| 126 | + prodh = '001000010000000100' ) |
| 127 | + ( text = 'Pump 002' |
| 128 | + prodh = '001000010000000105' ) |
| 129 | + ) |
| 130 | + ) ) |
| 131 | + ) |
| 132 | + ( text = 'Paints' |
| 133 | + prodh = '00110' |
| 134 | + nodes = VALUE #( ( text = 'Gloss paints' |
| 135 | + prodh = '0011000105' |
| 136 | + nodes = VALUE #( ( text = 'Paint 001' |
| 137 | + prodh = '001100010500000100' ) |
| 138 | + ( text = 'Paint 002' |
| 139 | + prodh = '001100010500000105' ) |
| 140 | + ) |
| 141 | + ) ) |
| 142 | + ) ). |
| 143 | +
|
| 144 | + DATA(page) = z2ui5_cl_xml_view=>factory( )->page( ). |
| 145 | + |
| 146 | + page->tree( items = client->_bind_edit( prodh_nodes ) |
| 147 | + )->items( |
| 148 | + )->standard_tree_item( selected = '{IS_SELECTED}' |
| 149 | + title = '{TEXT}' ). |
| 150 | + |
| 151 | + client->view_display( page->button( text = 'Open Popup here...' |
| 152 | + press = client->_event( 'POPUP_TREE' ) )->stringify( ) ). |
| 153 | +
|
| 154 | + ENDMETHOD. |
| 155 | +ENDCLASS. |
| 156 | +``` |
0 commit comments