Периодически встречаются программы использующие ALV таблицу в виде иерархии, к примеру ME59N. Ниже будет пример того как сделать нечто подобное.
Для работы программы необходим GUI статус с тремя кнопками (см код.). Код не самый оптимальный, исключительно ради примера:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
REPORT zalv_pl. TYPE-POOLS: <icon>. TYPES: BEGIN OF ty_spfli_ex, icon_field TYPE icon_d, t_cell_type TYPE SALV_T_INT4_COLUMN, t_color TYPE lvc_t_scol. INCLUDE STRUCTURE spfli. TYPES: END OF ty_spfli_ex. CLASS lcl_report DEFINITION. PUBLIC SECTION. CLASS-DATA: gt_all_data TYPE STANDARD TABLE OF ty_spfli_ex, " Все выбранные данные gt_alv_data TYPE STANDARD TABLE OF ty_spfli_ex, " Данные которые отображены в ALV c учётом свернутых go_alv TYPE REF TO cl_salv_table, gv_field TYPE STRING, gv_icon_field TYPE LVC_FNAME, gv_first_field TYPE STRING. CLASS-METHODS: on_added_function FOR EVENT added_function OF cl_salv_events_table IMPORTING e_salv_function, on_link_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row, get_data, " Получение данных show_alv, " Настройка и отображение ALV таблицы hide_all, " Свернуть все show_all, " Развернуть все FORWARD, " Вперед set_key " Устанавливает ключевое поле по которому будем определять дубликаты IMPORTING i_field TYPE CSEQUENCE, set_column_key_color IMPORTING i_column TYPE CSEQUENCE i_clear TYPE abap_bool OPTIONAL, set_icon IMPORTING i_icon TYPE CSEQUENCE, show_by " развернуть указанную строку IMPORTING i_field TYPE ANY i_row TYPE I, hide_by " Свернуть строку IMPORTING i_field TYPE ANY i_row TYPE I. ENDCLASS. " Реализация обработчика события для двойного щелчка CLASS lcl_report IMPLEMENTATION. METHOD get_data. CLEAR gt_all_data. SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_all_data. ENDMETHOD. METHOD set_column_key_color. DATA: lo_columns TYPE REF TO cl_salv_columns_table, lv_color TYPE LVC_S_COLO, lo_column TYPE REF TO cl_salv_column_table. lo_columns = go_alv->get_columns( ). TRY. lo_column ?= lo_columns->get_column( i_column ). CATCH CX_SALV_NOT_FOUND. EXIT. ENDTRY. IF i_clear IS INITIAL. lv_color-col = 4. lv_color-int = 1. ENDIF. lo_column->set_color( lv_color ). ENDMETHOD. METHOD show_alv. DATA: lo_columns TYPE REF TO cl_salv_columns_table, lo_column TYPE REF TO cl_salv_column_table, lo_events TYPE REF TO cl_salv_events_table, lv_fname TYPE LVC_FNAME. IF go_alv IS NOT BOUND. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = go_alv CHANGING t_table = gt_alv_data ). CATCH cx_salv_msg . MESSAGE 'Ошибка при создании ALV' TYPE 'E'. ENDTRY. go_alv->set_screen_status( EXPORTING REPORT = 'ZTMP1' pfstatus = 'SALV_TABLE_STANDARD' set_functions = cl_salv_model_base=>c_functions_none ). " Настройка событий TRY. lo_events = go_alv->get_event( ). SET HANDLER on_added_function FOR lo_events. SET HANDLER on_link_click FOR lo_events. CATCH CX_SALV_NOT_FOUND. EXIT. ENDTRY. " Настройка колонок TRY. lo_columns = go_alv->get_columns( ). lo_columns->set_optimize( abap_true ). lo_columns->set_cell_type_column( 'T_CELL_TYPE' ). lo_column ?= lo_columns->get_column( 'MANDT' ). lo_column->set_technical( abap_true ). lo_column ?= lo_columns->get_column( 'CARRID' ). lo_column->set_key( abap_false ). lo_column ?= lo_columns->get_column( 'CONNID' ). lo_column->set_key( abap_false ). lo_column ?= lo_columns->get_column( gv_icon_field ). set_column_key_color( gv_icon_field ). lo_columns->set_color_column( 'T_COLOR' ). lv_fname = gv_field. lo_columns->set_column_position( EXPORTING columnname = lv_fname POSITION = 2 ). set_column_key_color( lv_fname ). CATCH CX_SALV_NOT_FOUND CX_SALV_DATA_ERROR. EXIT. ENDTRY. " Свернем все строки hide_all( ). go_alv->display( ). ELSE. " ALV создан - обновляем lv_fname = gv_field. lo_columns = go_alv->get_columns( ). lo_columns->set_column_position( EXPORTING columnname = lv_fname POSITION = 2 ). set_column_key_color( lv_fname ). lo_columns->set_column_position( EXPORTING columnname = gv_icon_field POSITION = 1 ). hide_all( ). go_alv->refresh( ). ENDIF. ENDMETHOD. METHOD hide_all. DATA: lv_index TYPE I, lv_cond TYPE STRING. FIELD-SYMBOLS: <fs_line> TYPE ty_spfli_ex, <fs_field> TYPE ANY, <fs_key> TYPE ANY, <fs_new_line> TYPE ty_spfli_ex. DATA: ls_type TYPE SALV_S_INT4_COLUMN. ls_type-columnname = gv_icon_field. ls_type-VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT. SORT gt_all_data BY (gv_field). CLEAR: gt_alv_data. LOOP AT gt_all_data ASSIGNING <fs_line>. UNASSIGN <fs_field>. ASSIGN COMPONENT gv_field OF STRUCTURE <fs_line> TO <fs_field>. CHECK <fs_field> IS ASSIGNED. IF ( <fs_key> IS NOT ASSIGNED ) OR ( <fs_key> <> <fs_field> ). ASSIGN <fs_field> TO <fs_key>. APPEND INITIAL LINE TO gt_alv_data ASSIGNING <fs_new_line>. MOVE-CORRESPONDING <fs_line> TO <fs_new_line>. lv_index = 0. lv_cond = <fs_field>. CONCATENATE gv_field ' = `' lv_cond '`' INTO lv_cond. LOOP AT gt_all_data TRANSPORTING NO FIELDS WHERE (lv_cond). lv_index = lv_index + 1. IF lv_index > 1. <fs_new_line>-icon_field = icon_positive. APPEND ls_type TO <fs_new_line>-t_cell_type. EXIT. ENDIF. ENDLOOP. ENDIF. ENDLOOP. go_alv->refresh( ). ENDMETHOD. METHOD show_by. DATA: lv_index TYPE I, s_scol TYPE lvc_s_scol, lv_cond TYPE STRING, ls_line TYPE ty_spfli_ex. FIELD-SYMBOLS <fs_line> TYPE ty_spfli_ex. DATA: ls_type TYPE SALV_S_INT4_COLUMN. ls_type-columnname = gv_icon_field. ls_type-VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT. SORT gt_all_data BY (gv_field). lv_cond = i_field. CONCATENATE gv_field ` = '` lv_cond `'` INTO lv_cond. lv_index = 0. LOOP AT gt_all_data INTO ls_line WHERE (lv_cond). lv_index = lv_index + 1. IF lv_index > 1. CLEAR s_scol. s_scol-color-col = 4. APPEND s_scol TO ls_line-t_color. s_scol-color-col = 4. s_scol-color-int = 1. s_scol-fname = gv_icon_field. APPEND s_scol TO ls_line-t_color. s_scol-color-col = 4. s_scol-color-int = 1. s_scol-fname = gv_field. APPEND s_scol TO ls_line-t_color. INSERT ls_line INTO gt_alv_data INDEX i_row + lv_index - 1. ENDIF. ENDLOOP. READ TABLE gt_alv_data INDEX i_row ASSIGNING <fs_line>. IF <fs_line> IS ASSIGNED. <fs_line>-icon_field = icon_negative. APPEND ls_type TO <fs_line>-t_cell_type. ENDIF. go_alv->refresh( ). ENDMETHOD. METHOD hide_by. DATA: lv_cond TYPE STRING. FIELD-SYMBOLS <fs_line> TYPE ty_spfli_ex. DATA: ls_type TYPE SALV_S_INT4_COLUMN. ls_type-columnname = gv_icon_field. ls_type-VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT. lv_cond = i_field. CONCATENATE '`' lv_cond '`' INTO lv_cond. CONCATENATE gv_field '=' lv_cond 'AND' gv_icon_field 'IS INITIAL' INTO lv_cond SEPARATED BY SPACE. DELETE gt_alv_data WHERE (lv_cond). READ TABLE gt_alv_data INDEX i_row ASSIGNING <fs_line>. IF <fs_line> IS ASSIGNED. <fs_line>-icon_field = icon_positive. APPEND ls_type TO <fs_line>-t_cell_type. ENDIF. go_alv->refresh( ). ENDMETHOD. METHOD set_key. gv_field = i_field. IF gv_first_field IS INITIAL. gv_first_field = i_field. ENDIF. ENDMETHOD. METHOD set_icon. gv_icon_field = i_icon. ENDMETHOD. METHOD on_link_click. FIELD-SYMBOLS: <fs_line> TYPE ty_spfli_ex, <fs_field> TYPE ANY. READ TABLE gt_alv_data INDEX row ASSIGNING <fs_line>. IF <fs_line> IS ASSIGNED. ASSIGN COMPONENT gv_field OF STRUCTURE <fs_line> TO <fs_field>. CHECK <fs_field> IS ASSIGNED. IF <fs_line>-icon_field = icon_negative. hide_by( EXPORTING i_field = <fs_field> i_row = row ). ELSEIF <fs_line>-icon_field = icon_positive. show_by( EXPORTING i_field = <fs_field> i_row = row ). ENDIF. ENDIF. ENDMETHOD. METHOD on_added_function. CASE e_salv_function. WHEN 'FORW'. FORWARD( ). WHEN 'HIALL'. hide_all( ). show_alv( ). WHEN 'SHALL'. show_all( ). ENDCASE. ENDMETHOD. METHOD FORWARD. DATA: lo_descr TYPE REF TO cl_abap_structdescr, lv_flag TYPE abap_bool, lv_lines TYPE I, lv_fname TYPE LVC_FNAME, lv_prev TYPE STRING, ls_descr TYPE abap_compdescr. lv_fname = gv_field. set_column_key_color( i_column = lv_fname i_clear = abap_true ). lo_descr ?= cl_abap_structdescr=>describe_by_name( 'ty_spfli_ex' ). lv_lines = LINES( lo_descr->components ). LOOP AT lo_descr->components INTO ls_descr. IF ( ls_descr-NAME = gv_field ) AND ( sy-tabix <> lv_lines ). lv_flag = abap_true. CONTINUE. ELSEIF ( sy-tabix = lv_lines ) AND ( ls_descr-NAME = gv_field ). set_key( gv_first_field ). lv_fname = gv_first_field. set_column_key_color( i_column = lv_fname ). show_alv( ). EXIT. ENDIF. IF lv_flag = abap_true. set_key( ls_descr-NAME ). lv_fname = ls_descr-NAME. set_column_key_color( i_column = lv_fname ). show_alv( ). EXIT. ENDIF. lv_prev = ls_descr-NAME. ENDLOOP. ENDMETHOD. METHOD show_all. DATA: lv_index TYPE I, s_scol TYPE lvc_s_scol, lv_cond TYPE STRING. FIELD-SYMBOLS: <fs_line> TYPE ty_spfli_ex, <fs_field> TYPE ANY, <fs_key> TYPE ANY, <fs_new_line> TYPE ty_spfli_ex. DATA: ls_type TYPE SALV_S_INT4_COLUMN. ls_type-columnname = gv_icon_field. ls_type-VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT. SORT gt_all_data BY (gv_field). CLEAR: gt_alv_data. LOOP AT gt_all_data ASSIGNING <fs_line>. UNASSIGN <fs_field>. ASSIGN COMPONENT gv_field OF STRUCTURE <fs_line> TO <fs_field>. CHECK <fs_field> IS ASSIGNED. APPEND INITIAL LINE TO gt_alv_data ASSIGNING <fs_new_line>. MOVE-CORRESPONDING <fs_line> TO <fs_new_line>. IF ( <fs_key> IS NOT ASSIGNED ) OR ( <fs_key> <> <fs_field> ). ASSIGN <fs_field> TO <fs_key>. lv_index = 0. lv_cond = <fs_field>. CONCATENATE gv_field ' = `' lv_cond '`' INTO lv_cond. LOOP AT gt_all_data TRANSPORTING NO FIELDS WHERE (lv_cond). lv_index = lv_index + 1. IF lv_index > 1. <fs_new_line>-icon_field = icon_negative. APPEND ls_type TO <fs_new_line>-t_cell_type. EXIT. ENDIF. ENDLOOP. ELSEIF ( <fs_key> IS ASSIGNED ) AND ( <fs_key> = <fs_field> ). CLEAR s_scol. s_scol-color-col = 4. APPEND s_scol TO <fs_new_line>-t_color. s_scol-color-col = 4. s_scol-color-int = 1. s_scol-fname = gv_icon_field. APPEND s_scol TO <fs_new_line>-t_color. s_scol-color-col = 4. s_scol-color-int = 1. s_scol-fname = gv_field. APPEND s_scol TO <fs_new_line>-t_color. ENDIF. ENDLOOP. go_alv->refresh( ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. lcl_report=>get_data( ). lcl_report=>set_key( 'CARRID' ). lcl_report=>set_icon( 'ICON_FIELD' ). lcl_report=>show_alv( ). |
Я прошу прощения, но в какой версии это сделано? Потому как на вот такую сентенцию
LOOP AT gt_all_data INTO ls_line WHERE (lv_cond).
lv_index = lv_index + 1.
IF lv_index > 1.
CLEAR s_scol.
s_scol-color-col = 4.
APPEND s_scol TO ls_line-t_color.
s_scol-color-col = 4.
s_scol-color-int = 1.
s_scol-fname = gv_icon_field.
APPEND s_scol TO ls_line-t_color.
s_scol-color-col = 4.
s_scol-color-int = 1.
s_scol-fname = gv_field.
APPEND s_scol TO ls_line-t_color.
INSERT ls_line INTO gt_alv_data INDEX i_row + lv_index — 1.
ENDIF.
ENDLOOP.
Компилятор ругается вот таким вот матом цитирую
ABAP-оператор с «…(LV_COND)» в конце неожиданно завершен
конец цитаты
Заранее спасибо
Написано в 7.0 версии, с какой версии появились динамические условия в циклах увы не помню, могу рекомендовать посмотреть тут: help.sap.com/abapdocu_731/en/abennews.htm или в курсе по изменениям в ABAP.
аналогичная ошибка с lv_cond. Как бы ее побороть, подскажите, плиз…