📜 ⬆️ ⬇️

ABAP: Beautiful

This publication is intended for ABAP developers in SAP ERP and all of them sympathetic.

Few people know that you can include HTML headers in fullscreen ALV. They don’t even know that you can make a beautiful standard drop-down list, it’s a select-box, only for such a standard feature, you need a lot of your Z-code.

It looks like this:
')


Welcome under cat.

Go! Define global variables:

- The output table of our report, let it be based on the well-known MARA table;
- A variable in which we will store the current value selected in the select box;
- constant with subroutine for HTML header;
- the handler class that will be triggered when the data is selected, and the handler object.

*----------------------------------------------------------------------* *    *----------------------------------------------------------------------* TYPE-POOLS: slis. TABLES: mara. DATA: gt_data TYPE TABLE OF mara WITH HEADER LINE, gv_matnr TYPE mara-matnr. CONSTANTS: gc_form_top TYPE slis_formname VALUE 'DO_HTML_TOP_OF_PAGE'. *----------------------------------------------------------------------* * CLASS cl_my_event_handler DEFINITION *----------------------------------------------------------------------* CLASS cl_my_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_selections FOR EVENT selected OF cl_dd_select_element IMPORTING sender. ENDCLASS. DATA: go_hand1 TYPE REF TO cl_my_event_handler. 

Let's write the main program of the report and the subroutine on initialization, data acquisition and report output. Here I’ll especially note that you need to create a handler object and connect the HTML header: i_callback_html_top_of_page = gc_form_top.

 *----------------------------------------------------------------------* *  *----------------------------------------------------------------------* SELECT-OPTIONS: so_matnr FOR mara-matnr. INITIALIZATION. PERFORM init. START-OF-SELECTION. PERFORM get_data. END-OF-SELECTION. PERFORM reuse_alv. *&---------------------------------------------------------------------* *& Form init *&---------------------------------------------------------------------* FORM init. " -------------------------------------------------- " "    " -------------------------------------------------- " CREATE OBJECT go_hand1. ENDFORM. *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* FORM get_data. " -------------------------------------------------- " "   " -------------------------------------------------- " SELECT * FROM mara INTO TABLE gt_data WHERE matnr IN so_matnr. CHECK: sy-subrc IS INITIAL. SORT: gt_data BY matnr. ENDFORM. "get_data *&---------------------------------------------------------------------* *& Form reuse_alv *&---------------------------------------------------------------------* FORM reuse_alv. DATA: lt_fieldcat TYPE TABLE OF lvc_s_fcat. CHECK gt_data[] IS NOT INITIAL. " -------------------------------------------------- " "  ALV " -------------------------------------------------- " CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' EXPORTING i_structure_name = 'MARA' CHANGING ct_fieldcat = lt_fieldcat[] EXCEPTIONS inconsistent_interface = 1 program_error = 2 OTHERS = 3. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid i_callback_html_top_of_page = gc_form_top it_fieldcat_lvc = lt_fieldcat[] i_save = 'A' TABLES t_outtab = gt_data[]. ENDFORM. "reuse_alv 

For an HTML header, we create a subroutine with the name specified in the global constant. Let's call from it a subroutine that will create our beauty:

 *&---------------------------------------------------------------------* *& Form do_html_top_of_page *&---------------------------------------------------------------------* FORM do_html_top_of_page USING p_doc TYPE REF TO cl_dd_document. DATA: lo_form TYPE REF TO cl_dd_form_area, lo_sele TYPE REF TO cl_dd_select_element. " -------------------------------------------------- " "   " -------------------------------------------------- " CALL METHOD p_doc->add_form IMPORTING formarea = lo_form. " -------------------------------------------------- " "  - " -------------------------------------------------- " PERFORM add_casebox TABLES gt_data CHANGING gv_matnr lo_form lo_sele. ENDFORM. "do_html_top_of_page 

Let's look at the sub-box for creating select boxes. Here I’ll note that if there was a command in the report, remove the filters, clean up our global variable (gv_matnr) with a value. Then we create a line to which we add the filter header, call a subroutine that will fill in the values ​​in it, and the filter itself on the form, close the line:

 *&---------------------------------------------------------------------* *& Form add_casebox *&---------------------------------------------------------------------* FORM add_casebox *&---------------------------------------------------------------------* TABLES it_data STRUCTURE mara CHANGING iv_matnr TYPE mara-matnr lo_form TYPE REF TO cl_dd_form_area lo_selec TYPE REF TO cl_dd_select_element. *&---------------------------------------------------------------------* DATA: lt_opt_tab TYPE sdydo_option_tab, lv_text TYPE sdydo_text_element. *----------------------------------------------------------------------* DO. CASE sy-index. WHEN 1. " -------------------------------------------------- " "   " -------------------------------------------------- " CHECK sy-ucomm EQ '&ILD'. CLEAR: iv_matnr. WHEN 2. " -------------------------------------------------- " "   " -------------------------------------------------- " CALL METHOD lo_form->line_with_layout EXPORTING start = 'X'. WHEN 3. " -------------------------------------------------- " "  - " -------------------------------------------------- " lv_text = ':'. CALL METHOD lo_form->add_text EXPORTING text = lv_text. WHEN 4. " -------------------------------------------------- " "  " -------------------------------------------------- " CALL METHOD lo_form->add_gap EXPORTING width = 2. WHEN 5. " -------------------------------------------------- " "    " -------------------------------------------------- " PERFORM fill_mat_tab TABLES it_data CHANGING iv_matnr lt_opt_tab. WHEN 6. " -------------------------------------------------- " "   - " -------------------------------------------------- " CALL METHOD lo_form->add_select_element EXPORTING OPTIONS = lt_opt_tab value = 'P' IMPORTING select_element = lo_selec. WHEN 7. " -------------------------------------------------- " "   " -------------------------------------------------- " SET HANDLER go_hand1->handle_selections FOR lo_selec. WHEN 8. " -------------------------------------------------- " "   " -------------------------------------------------- " CALL METHOD lo_form->line_with_layout EXPORTING end = 'X'. WHEN OTHERS. EXIT. ENDCASE. ENDDO. ENDFORM. 

In the filter filling program, first add the value that is currently selected so that it is first in the list. Then we add the value All, if we have more than one value in the table. And then all the records from the table:

 *&---------------------------------------------------------------------* *& Form fill_mat_tab *&---------------------------------------------------------------------* FORM fill_mat_tab *&---------------------------------------------------------------------* TABLES it_data STRUCTURE mara CHANGING iv_matnr TYPE matnr it_optab TYPE sdydo_option_tab. *&---------------------------------------------------------------------* DATA: ls_opt TYPE sdydo_option. REFRESH: it_optab. *----------------------------------------------------------------------* DO. CASE sy-index. WHEN 1. " -------------------------------------------------- " "      " -------------------------------------------------- " CHECK iv_matnr IS NOT INITIAL. ls_opt-value = iv_matnr. ls_opt-text = iv_matnr. APPEND ls_opt TO it_optab. WHEN 2. " -------------------------------------------------- " "    ,   -  " -------------------------------------------------- " READ TABLE it_data INDEX 1. LOOP AT it_data TRANSPORTING NO FIELDS WHERE matnr NE it_data-matnr. ls_opt-value = '*'. ls_opt-text = ''. APPEND ls_opt TO it_optab. EXIT. ENDLOOP. WHEN 3. " -------------------------------------------------- " "    " -------------------------------------------------- " LOOP AT it_data WHERE matnr IS NOT INITIAL. ls_opt-value = it_data-matnr. ls_opt-text = it_data-matnr. COLLECT ls_opt INTO it_optab. ENDLOOP. WHEN OTHERS. EXIT. ENDCASE. ENDDO. ENDFORM. 

Introduce our handler. Here in sender-> value is the value selected by the user. We write it immediately in our global variable gv_matnr. In the set_filter routine for processing standard filtering:

1) Get a global grid into a local object;
2) We will get the already set filtering parameters, and remove the already installed filter by the field that we are filtering through the select box;
3) Add new filtering options;
4) Save the filter;
5) Update the report.

 *----------------------------------------------------------------------* * CLASS cl_my_event_handler IMPLEMENTATION *----------------------------------------------------------------------* CLASS cl_my_event_handler IMPLEMENTATION. METHOD handle_selections. DATA text_buff TYPE sdydo_text_element. text_buff = sender->value. gv_matnr = text_buff. " -------------------------------------------------- " "     " -------------------------------------------------- " PERFORM set_filter TABLES gt_data USING gv_matnr . ENDMETHOD. "handle_selections ENDCLASS. "cl_my_event_handler IMPLEMENTATION *&---------------------------------------------------------------------* *& Form set_filter *&---------------------------------------------------------------------* FORM set_filter *&---------------------------------------------------------------------* TABLES it_data STRUCTURE mara USING iv_value TYPE mara-matnr. *&---------------------------------------------------------------------* DATA: lo_ref1 TYPE REF TO cl_gui_alv_grid, lt_filtered TYPE lvc_t_filt, lv_field TYPE char10 VALUE 'MATNR', ls_filter LIKE LINE OF lt_filtered. *----------------------------------------------------------------------* DO. CASE sy-index. WHEN 1. " -------------------------------------------------- " "   ALV " -------------------------------------------------- " CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = lo_ref1. WHEN 2. " -------------------------------------------------- " "      .    " -------------------------------------------------- " CALL METHOD lo_ref1->get_filter_criteria IMPORTING et_filter = lt_filtered. DELETE lt_filtered WHERE fieldname = lv_field. WHEN 3. " -------------------------------------------------- " "    " -------------------------------------------------- " CHECK iv_value NE '' AND iv_value NE '*'. ls_filter-fieldname = lv_field. ls_filter-sign = 'I'. ls_filter-option = 'EQ'. ls_filter-low = iv_value. APPEND ls_filter TO lt_filtered. WHEN 4. " -------------------------------------------------- " "   " -------------------------------------------------- " CALL METHOD lo_ref1->set_filter_criteria EXPORTING it_filter = lt_filtered EXCEPTIONS no_fieldcatalog_available = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. WHEN 5. " -------------------------------------------------- " "  ALV " -------------------------------------------------- " lo_ref1->refresh_table_display( ). WHEN OTHERS. EXIT. ENDCASE. ENDDO. ENDFORM. 

Run! We admire the result of suffering:





UPDATE: can be brought to:

It seems to be a simple thing, but in SAP you need to try again. Thanks to all.

Source: https://habr.com/ru/post/275677/


All Articles