|
| 1 | +CLASS z2ui5_cl_demo_app_421 DEFINITION PUBLIC. |
| 2 | + |
| 3 | + PUBLIC SECTION. |
| 4 | + INTERFACES z2ui5_if_app. |
| 5 | + |
| 6 | + TYPES: |
| 7 | + BEGIN OF ty_s_row, |
| 8 | + index TYPE i, |
| 9 | + title TYPE string, |
| 10 | + value TYPE string, |
| 11 | + info TYPE string, |
| 12 | + checkbox TYPE abap_bool, |
| 13 | + description TYPE string, |
| 14 | + END OF ty_s_row. |
| 15 | + DATA t_tab TYPE STANDARD TABLE OF ty_s_row WITH EMPTY KEY. |
| 16 | + |
| 17 | + DATA focuscolumn TYPE string. |
| 18 | + DATA focusrow TYPE string. |
| 19 | + DATA focusid TYPE string READ-ONLY. |
| 20 | + |
| 21 | + PROTECTED SECTION. |
| 22 | + CONSTANTS: |
| 23 | + BEGIN OF cs_column, |
| 24 | + title TYPE string VALUE `Title`, |
| 25 | + color TYPE string VALUE `Color`, |
| 26 | + info TYPE string VALUE `Info`, |
| 27 | + checkbox TYPE string VALUE `Checkbox`, |
| 28 | + description TYPE string VALUE `Description`, |
| 29 | + END OF cs_column. |
| 30 | + |
| 31 | + DATA client TYPE REF TO z2ui5_if_client. |
| 32 | + |
| 33 | + METHODS on_init. |
| 34 | + METHODS on_event. |
| 35 | + METHODS view_display. |
| 36 | + METHODS focus. |
| 37 | + METHODS read_focus. |
| 38 | + METHODS next_focus. |
| 39 | + METHODS default_focus. |
| 40 | + |
| 41 | + PRIVATE SECTION. |
| 42 | +ENDCLASS. |
| 43 | + |
| 44 | + |
| 45 | +CLASS z2ui5_cl_demo_app_421 IMPLEMENTATION. |
| 46 | + |
| 47 | + METHOD z2ui5_if_app~main. |
| 48 | + |
| 49 | + me->client = client. |
| 50 | + |
| 51 | + IF client->check_on_init( ). |
| 52 | + on_init( ). |
| 53 | + ELSEIF client->check_on_navigated( ). |
| 54 | + view_display( ). |
| 55 | + ELSEIF client->check_on_event( ). |
| 56 | + on_event( ). |
| 57 | + ENDIF. |
| 58 | + |
| 59 | + ENDMETHOD. |
| 60 | + |
| 61 | + |
| 62 | + METHOD on_init. |
| 63 | + |
| 64 | + t_tab = VALUE #( |
| 65 | + ( index = 0 title = `entry 01` value = `red` info = `completed` description = `this is a description` checkbox = abap_true ) |
| 66 | + ( index = 1 title = `entry 02` value = `blue` info = `completed` description = `this is a description` checkbox = abap_true ) |
| 67 | + ( index = 2 title = `entry 03` value = `green` info = `completed` description = `this is a description` checkbox = abap_true ) |
| 68 | + ( index = 3 title = `entry 04` value = `orange` info = `completed` description = `` checkbox = abap_true ) |
| 69 | + ( index = 4 title = `entry 05` value = `grey` info = `completed` description = `this is a description` checkbox = abap_true ) |
| 70 | + ( index = 5 ) ). |
| 71 | + |
| 72 | + default_focus( ). |
| 73 | + view_display( ). |
| 74 | + focus( ). |
| 75 | + |
| 76 | + ENDMETHOD. |
| 77 | + |
| 78 | + |
| 79 | + METHOD on_event. |
| 80 | + |
| 81 | + CASE client->get( )-event. |
| 82 | + WHEN `FOCUS`. |
| 83 | + focus( ). |
| 84 | + WHEN `NEXT`. |
| 85 | + read_focus( ). |
| 86 | + next_focus( ). |
| 87 | + focus( ). |
| 88 | + WHEN `RESET`. |
| 89 | + default_focus( ). |
| 90 | + focus( ). |
| 91 | + ENDCASE. |
| 92 | + |
| 93 | + client->view_model_update( ). |
| 94 | + |
| 95 | + ENDMETHOD. |
| 96 | + |
| 97 | + |
| 98 | + METHOD view_display. |
| 99 | + |
| 100 | + DATA(view) = z2ui5_cl_xml_view=>factory( ). |
| 101 | + |
| 102 | + DATA(page) = view->shell( |
| 103 | + )->page( |
| 104 | + title = `abap2UI5 - Focus a Table Cell` |
| 105 | + navbuttonpress = client->_event_nav_app_leave( ) |
| 106 | + shownavbutton = client->check_app_prev_stack( ) ). |
| 107 | + |
| 108 | + page->message_strip( |
| 109 | + text = `Set the keyboard focus to any editable table cell from the backend - type a column id ` && |
| 110 | + `(Title, Color, Info, Checkbox or Description) and a row index, then press Set Focus, or ` && |
| 111 | + `use Next / Reset. No JavaScript is shipped with the view: every cell has a stable control ` && |
| 112 | + `id (<column>_<row>) that the set_focus follow-up action targets, and the framework reports ` && |
| 113 | + `the currently focused cell back to the backend in s_focus.` |
| 114 | + type = `Information` |
| 115 | + showicon = abap_true |
| 116 | + class = `sapUiSmallMargin` ). |
| 117 | + |
| 118 | + DATA(tab) = page->table( |
| 119 | + )->header_toolbar( |
| 120 | + )->overflow_toolbar( |
| 121 | + )->title( client->_bind( focusid ) |
| 122 | + )->toolbar_spacer( |
| 123 | + )->label( `Column Id` |
| 124 | + )->input( |
| 125 | + value = client->_bind_edit( focuscolumn ) |
| 126 | + submit = client->_event( `FOCUS` ) |
| 127 | + placeholder = `Column` |
| 128 | + width = `8rem` |
| 129 | + )->label( `Row Index` |
| 130 | + )->input( |
| 131 | + value = client->_bind_edit( focusrow ) |
| 132 | + submit = client->_event( `FOCUS` ) |
| 133 | + placeholder = `Row` |
| 134 | + type = `Number` |
| 135 | + width = `6rem` |
| 136 | + )->button( |
| 137 | + text = `Set Focus` |
| 138 | + press = client->_event( `FOCUS` ) |
| 139 | + )->button( |
| 140 | + text = `Next Focus` |
| 141 | + press = client->_event( `NEXT` ) |
| 142 | + )->button( |
| 143 | + text = `Reset Focus` |
| 144 | + press = client->_event( `RESET` ) |
| 145 | + )->get_parent( )->get_parent( ). |
| 146 | + |
| 147 | + tab->columns( |
| 148 | + )->column( )->text( `Index` )->get_parent( |
| 149 | + )->column( )->text( `Title` )->get_parent( |
| 150 | + )->column( )->text( `Color` )->get_parent( |
| 151 | + )->column( )->text( `Info` )->get_parent( |
| 152 | + )->column( )->text( `Checkbox` )->get_parent( |
| 153 | + )->column( )->text( `Description` ). |
| 154 | + |
| 155 | + " Build the rows explicitly (no aggregation binding): only then does every |
| 156 | + " cell keep the stable control id <column>_<row> that set_focus can target. |
| 157 | + " A bound template would clone the cells under randomly generated ids. |
| 158 | + DATA(path) = client->_bind_edit( val = t_tab path = abap_true ). |
| 159 | + DATA(items) = tab->items( ). |
| 160 | + |
| 161 | + LOOP AT t_tab REFERENCE INTO DATA(row). |
| 162 | + |
| 163 | + DATA(i) = sy-tabix - 1. |
| 164 | + |
| 165 | + items->column_list_item( |
| 166 | + )->cells( |
| 167 | + )->text( |{ row->index }| |
| 168 | + )->input( |
| 169 | + id = |{ cs_column-title }_{ i }| |
| 170 | + value = |\{{ path }/{ i }/TITLE\}| |
| 171 | + submit = client->_event( `NEXT` ) |
| 172 | + )->input( |
| 173 | + id = |{ cs_column-color }_{ i }| |
| 174 | + value = |\{{ path }/{ i }/VALUE\}| |
| 175 | + submit = client->_event( `NEXT` ) |
| 176 | + )->input( |
| 177 | + id = |{ cs_column-info }_{ i }| |
| 178 | + value = |\{{ path }/{ i }/INFO\}| |
| 179 | + submit = client->_event( `NEXT` ) |
| 180 | + )->checkbox( |
| 181 | + id = |{ cs_column-checkbox }_{ i }| |
| 182 | + selected = |\{{ path }/{ i }/CHECKBOX\}| |
| 183 | + )->input( |
| 184 | + id = |{ cs_column-description }_{ i }| |
| 185 | + value = |\{{ path }/{ i }/DESCRIPTION\}| |
| 186 | + submit = client->_event( `NEXT` ) ). |
| 187 | + |
| 188 | + ENDLOOP. |
| 189 | + |
| 190 | + client->view_display( view->stringify( ) ). |
| 191 | + |
| 192 | + ENDMETHOD. |
| 193 | + |
| 194 | + |
| 195 | + METHOD focus. |
| 196 | + |
| 197 | + focusid = |{ focuscolumn }_{ focusrow }|. |
| 198 | + |
| 199 | + client->follow_up_action( |
| 200 | + val = z2ui5_if_client=>cs_event-set_focus |
| 201 | + t_arg = VALUE #( ( focusid ) ) ). |
| 202 | + |
| 203 | + ENDMETHOD. |
| 204 | + |
| 205 | + |
| 206 | + METHOD read_focus. |
| 207 | + |
| 208 | + SPLIT client->get( )-s_focus-id AT `_` INTO DATA(col) DATA(row). |
| 209 | + |
| 210 | + IF row IS NOT INITIAL |
| 211 | + AND row CO `0123456789` |
| 212 | + AND ( col = cs_column-title |
| 213 | + OR col = cs_column-color |
| 214 | + OR col = cs_column-info |
| 215 | + OR col = cs_column-checkbox |
| 216 | + OR col = cs_column-description ). |
| 217 | + |
| 218 | + focuscolumn = col. |
| 219 | + focusrow = row. |
| 220 | + ENDIF. |
| 221 | + |
| 222 | + ENDMETHOD. |
| 223 | + |
| 224 | + |
| 225 | + METHOD next_focus. |
| 226 | + |
| 227 | + focuscolumn = SWITCH #( focuscolumn |
| 228 | + WHEN cs_column-title THEN cs_column-color |
| 229 | + WHEN cs_column-color THEN cs_column-info |
| 230 | + WHEN cs_column-info THEN cs_column-checkbox |
| 231 | + WHEN cs_column-checkbox THEN cs_column-description |
| 232 | + ELSE cs_column-title ). |
| 233 | + |
| 234 | + IF focuscolumn = cs_column-title. |
| 235 | + |
| 236 | + DATA(nextrow) = CONV i( focusrow ) + 1. |
| 237 | + IF line_exists( t_tab[ nextrow + 1 ] ). |
| 238 | + focusrow = |{ nextrow }|. |
| 239 | + ELSE. |
| 240 | + focusrow = `0`. |
| 241 | + ENDIF. |
| 242 | + ENDIF. |
| 243 | + |
| 244 | + ENDMETHOD. |
| 245 | + |
| 246 | + |
| 247 | + METHOD default_focus. |
| 248 | + |
| 249 | + focuscolumn = cs_column-title. |
| 250 | + focusrow = `0`. |
| 251 | + |
| 252 | + ENDMETHOD. |
| 253 | + |
| 254 | +ENDCLASS. |
0 commit comments