A Comprehensive Guide to Interactive SAP ALV

Extensive ALV Report Programming

Having worked on a variety of projects and utilizing interactive SAP ALV OOPs ideas extensively, I had the idea to compile a document that would provide helpful information to help developers meet most of the design requirements found in interactive ALV reports.

Now let’s begin our topic for today.

Make the Container Reference Object:

Type “ALV_CONT” in the container that appears on the screen.

CREATE OBJECT container_r
        EXPORTING
          container_name              = 'ALV_CONT'
        EXCEPTIONS
          cntl_error                  = 1
          cntl_system_error           = 2
          create_error                = 3
          lifetime_error              = 4
          lifetime_dynpro_dynpro_link = 5.

As shown below, create the ALV Grid’s Reference Object:

CREATE OBJECT grid_r
          EXPORTING
            i_parent = container_r.

4. Define ALV Layout as below:

gw_layout-cwidth_opt = abap_true.
gw_layout-zebra = abap_true.
gw_layout-sel_mode = ‘D’.

5. Enter data into the Field Catalog such that it appears as follows:
Regarding Field Editable: 5.1Furthermore, the field (such as ANLKL) that will host the summation must be defined as follows:

lv_pos = lv_pos + 1.              “Position number
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'BUKRS'.  "Company Code
wa_fieldcat-col_pos = lv_pos.
wa_fieldcat-ref_table = 'T001'.
APPEND wa_fieldcat TO gt_fieldcat.

5.1. For Field Editable:

wa_fieldcat-edit = abap_true.

5.2. For the output field “No Display”:

wa_fieldcat-no_out = abap_true.   “Check for field display

5.3. To show the field as a checkbox:

wa_fieldcat-checkbox = abap_true.

5.4. For the field that needs customized F4-Help:

wa_fieldcat-f4availabl  = abap_true.

5.5. For the field that needs the summary:

wa_fieldcat-do_sum = abap_true.

Furthermore, the field (such as ANLKL) that will host the summation must be defined as follows:

CLEAR li_sort.

lst_sort-fieldname = 'ANLKL'.

lst_sort-subtot = abap_true.

APPEND lst_sort TO li_sort

6. To Limit Features on ALV Display:

lw_exclude = cl_gui_alv_grid=>mc_fc_check.
APPEND lw_exclude TO gt_exclude.
lw_exclude = cl_gui_alv_grid=>mc_fc_refresh.
APPEND lw_exclude TO gt_exclude.
lw_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
APPEND lw_exclude TO gt_exclude.
lw_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
APPEND lw_exclude TO gt_exclude.
lw_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND lw_exclude TO gt_exclude.
lw_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
APPEND lw_exclude TO gt_exclude.

7 Add data to the GT_DATA table.

8 To display data, use the set_table_for_first_display function.

CALL METHOD grid_r->set_table_for_first_display
EXPORTING
i_structure_name     = 'ZFI_S_PIS'  “Structure
is_layout            = gw_layout
it_toolbar_excluding = gt_exclude
i_save               = gc_save_a   "A
i_default            = abap_true
CHANGING
it_outtab            = gt_data[]     “Table with ALV data

It_sort              = li_sort
it_fieldcatalog      = gt_fieldcat[].
CALL METHOD cl_gui_control=>set_focus
EXPORTING
control = grid_r.

Data declaration for Reference:

Data:

container_r TYPE REF TO cl_gui_custom_container,      "Container for ALV PIS data

grid_r TYPE REF TO cl_gui_alv_grid,                                "Grid for ALV PIS data

gt_exclude TYPE ui_functions,                                            "Exclude functions from ALV

gt_fieldcat TYPE lvc_t_fcat,                                                "Field catalog for ALV PIS data

ref_aplic TYPE REF TO lcl_application_alv,

gw_layout   TYPE lvc_s_layo.                                              "Layout for PIS

9. If there’s merely a data refresh required:

CALL METHOD grid_r->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS   = 2.

10. When invoking ALV for display, use the following approach to make fields editable:

CALL METHOD grid_r->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.  " enter.  "MC_EVT_MODIFIED

In order to verify the modified data in the grid, we must now complete the following tasks:

11.1. Specify the definition and implementation of a class (lcl_application_alv), and specify an event (handle_data_changed) for the data_changed event of the class CL_gui_alv_grid.

CLASS lcl_application_alv DEFINITION.
PUBLIC SECTION.
METHODS:
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed e_onf4_after.
ENDCLASS.                    "lcl_application_alv DEFINITION

11.2. To obtain and alter cell values as follows, invoke the methods get_cell_value and modify_cell in the handle_data_changed method.

LOOP AT er_data_changed->mt_good_cells INTO lw_good.

WHEN 'KUNNR'.
CALL METHOD er_data_changed->get_cell_value
EXPORTING
i_row_id    = lw_good-row_id
i_fieldname = lw_good-fieldname
IMPORTING
e_value     = gv_kunnr.
*     Get name1 for the kunnr
SELECT SINGLE name1 FROM kna1
INTO lw_pis-name1
WHERE kunnr = gv_kunnr .
IF sy-subrc = 0.
*              update name1 in table
CALL METHOD er_data_changed->modify_cell
EXPORTING
i_row_id    = lw_good-row_id
i_fieldname = 'NAME1'
i_value     = lv_name1.
ELSE.
MESSAGE i000 WITH 'Invalid Customer number in row'(027)
lw_good-row_id.
ENDIF.

12 Now, write the following code before invoking the set_table_for_first_display method:

CREATE OBJECT ref_aplic.
SET HANDLER ref_aplic->handle_data_changed FOR grid_r.

After the field catalog data is populated, call the set_frontend_fieldcatalog method to refresh the field catalog settings if the Field Catalog needs to change when different actions are performed for the ALV grid data presentation.

CALL METHOD grid_r->set_frontend_fieldcatalog
EXPORTING
it_fieldcatalog = gt_fieldcat[].

If the ALV requires that Conditional F4 Help data be entered, for example, because one field’s data requires that another field’s F4-Help data be entered, then take the following actions:

14.1. In order to receive customized F4-help, we must first register the fields as follows before invoking the set_table_for_first_display method:

lw_f4-fieldname  = 'ZFI_BRN_LOC'.    "IFSC code
lw_f4-register   = abap_true.
lw_f4-getbefore  = space.
lw_f4-chngeafter = abap_true.
INSERT lw_f4 INTO TABLE lt_f4.

CLEAR lw_f4.
lw_f4-fieldname  = 'STGRD'.  "Reason for Reversal
lw_f4-register   = abap_true.
lw_f4-getbefore  = space.
INSERT lw_f4 INTO  TABLE lt_f4.

CALL METHOD grid_r->register_f4_for_fields
EXPORTING
it_f4 = lt_f4.

14.2. In class lcl_application_alv, define the method and then implement it.

METHODS:
handle_on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING sender
e_fieldname
e_fieldvalue
es_row_no
er_event_data
et_bad_cells
e_display.

14.3. In class lcl_application_alv implementation, implement the method handle_on_f4.

*-------------------------------------------------------------
* Add the custom F4 values for IFSC code
*-------------------------------------------------------------
IF e_fieldname = 'ZFI_BRN_LOC'.
IF gv_flag_inst_type = abap_true.  "For chq only
*        IF gt_data[] IS INITIAL.
CLEAR lt_knbk.
CLEAR gt_data.
READ TABLE gt_pis INTO lw_pis INDEX es_row_no-row_id.
IF sy-subrc = 0.
SELECT bankl bkref FROM knbk
INTO TABLE lt_knbk WHERE kunnr = lw_pis-kunnr.  "gv_kunnr.
IF sy-subrc = 0.
LOOP AT lt_knbk INTO lw_knbk.
lw_data-zfi_brn_loc = lw_knbk-bankl.
lw_data-zfi_bnk_code = lw_knbk-bkref.
APPEND lw_data TO gt_data.
CLEAR lw_data.
ENDLOOP.
ENDIF.
ENDIF.
*        ENDIF.
*Call the function module to display the custom F4 values
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield        = 'ZFI_BRN_LOC'
window_title    = 'List of IFSC entries'(026)
value_org       = gc_val_org
TABLES
value_tab       = gt_data[]
return_tab      = lt_ret[]
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS          = 3.
*    (note: gt_ret[] contains the row id selected by the user from the list of f4 values)
IF sy-subrc = 0.
READ TABLE lt_ret INTO lw_sel INDEX 1.
ASSIGN er_event_data->m_data->* TO <itab>.
READ TABLE gt_pis INDEX es_row_no-row_id
INTO lw_pis.
lw_modi-row_id   = es_row_no-row_id.
lw_modi-fieldname = 'ZFI_BRN_LOC'.
lw_modi-value     = lw_sel-fieldval.
APPEND lw_modi TO <itab>.

READ TABLE gt_data INTO lw_data
WITH KEY zfi_brn_loc = lw_modi-value+0(11).
IF sy-subrc = 0.
lw_modi-row_id   = es_row_no-row_id.
lw_modi-fieldname = 'ZFI_BNK_CODE'.
lw_modi-value     = lw_data-zfi_bnk_code.
APPEND lw_modi TO <itab>.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
*-------------------------------------------------------------
* Add the custom F4 values for Reversal Reason
*-------------------------------------------------------------
IF e_fieldname = 'STGRD'.
CLEAR: lt_t041ct, lt_return.
IF gv_uname = gc_depo_userid.    "BTDEPO
SELECT stgrd txt40 FROM t041ct
INTO TABLE lt_t041ct
WHERE spras = sy-langu
AND stgrd = gc_stgrd_06.
IF sy-subrc = 0.
*    do nothing
ENDIF.
ELSE.
SELECT stgrd txt40 FROM t041ct
INTO TABLE lt_t041ct
WHERE spras = sy-langu
AND stgrd IN (gc_stgrd_06,
gc_stgrd_08,
gc_stgrd_09,
gc_stgrd_10).
IF sy-subrc = 0.
*    do nothing
ENDIF.
ENDIF.

IF sy-subrc = 0.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield        = 'STGRD'
window_title    = 'List of Rev reason entries'(025)
value_org       = gc_val_org
TABLES
value_tab       = lt_t041ct
return_tab      = lt_return
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS          = 3.
IF sy-subrc = 0.
READ TABLE lt_return INTO lw_sel INDEX 1.
ASSIGN er_event_data->m_data->* TO <itab>.
READ TABLE gt_pis INDEX es_row_no-row_id
INTO lw_pis.
lw_modi-row_id   = es_row_no-row_id.
lw_modi-fieldname = 'STGRD'.
lw_modi-value     = lw_sel-fieldval.
APPEND lw_modi TO <itab>.
ENDIF.
ENDIF.
ENDIF.
er_event_data->m_event_handled = abap_true. "(to inform grid that f4 was handled manually)

14.4. Add the following code when Register_F4_for_Fields is called::

SET HANDLER ref_aplic->handle_on_f4 FOR grid_r.

15. Clear and refresh as follows when you touch the Back button:

CALL METHOD grid_r->refresh_table_display.
CALL METHOD grid_r->free.
CALL METHOD container_r->free.
CALL METHOD cl_gui_cfw=>flush.

16. Add this code to an ALV to make it invisible:

CALL METHOD inv_grid_r->set_visible( EXPORTING visible = '0' ).

17. To use code to select a row: In lt_lvc_s_roid, pass the row ids.–

lw_lvc_s_roid-row_id = sy-tabix.
APPEND lw_lvc_s_roid TO lt_lvc_s_roid.

CALL METHOD grid_r->set_selected_rows
EXPORTING
it_row_no = lt_lvc_s_roid.

18. Add the following code to reset the scroll bar position after using code to select rows:

CALL METHOD grid_r->get_scroll_info_via_id
IMPORTING
es_row_no   = lw_row_no
es_row_info = lw_row_info
es_col_info = lw_col_info.

CALL METHOD grid_r->set_selected_rows
EXPORTING
it_row_no = lt_lvc_s_roid.

CALL METHOD grid_r->set_scroll_info_via_id(
EXPORTING
is_row_no   = lw_row_no
is_row_info = lw_row_info
is_col_info = lw_col_info
).

19. Display Top-of-Page:

19 The main container must first be divided into two parts in order to display the to-of-page details. One half will be allocated to the GRID, while the other will be used to populate the top header details, as seen below:

* Create TOP-Document

CREATE OBJECT g_dyndoc_id

EXPORTING

style = 'ALV_GRID'.

* Create Splitter for custom_container

CREATE OBJECT g_splitter

EXPORTING

parent  = g_custom_container

rows    = 2

columns = 1.
CALL METHOD g_splitter->get_container

EXPORTING

row       = 1

column    = 1

RECEIVING

container = g_parent_top.
CALL METHOD g_splitter->get_container

EXPORTING

row       = 2

column    = 1

RECEIVING

container = g_parent_grid.
* Set height for g_parent_html

CALL METHOD g_splitter->set_row_height

EXPORTING

id     = 1

height = 20.

CREATE OBJECT g_grid

EXPORTING

i_parent          = g_parent_grid

EXCEPTIONS

error_cntl_create = 1

error_cntl_init   = 2

error_cntl_link   = 3

error_dp_create   = 4

OTHERS            = 5.

19.2. Define the handle_top_of_page method as follows in the local class:

METHODS:

*Define method to handle Toolbar

handle_top_of_page FOR EVENT top_of_page

OF cl_gui_alv_grid

IMPORTING e_dyndoc_id.

19.3.Specify the method specifics in the class implementation:

METHOD handle_top_of_page.

PERFORM f_event_top_of_page USING g_dyndoc_id.

ENDMETHOD. "EVENT_HANDLER

19.4. Logic in the Subroutine:

FORM f_event_top_of_page USING   dg_dyndoc_id TYPE REF TO cl_dd_document.
DATA: lv_name    TYPE lvc_fname,  "Name

li_t093b   TYPE STANDARD TABLE OF ty_t093b

INITIAL SIZE 0,  "T093B temp table

lst_t093b  TYPE ty_t093b,  "T093b wa

lv_reptext TYPE reptext,  "report Text

lv_text    TYPE sdydo_text_element. "Final text
* Header1

lv_name = c_header1.

PERFORM f_read_text USING lv_name

CHANGING lv_reptext.

lv_text = lv_reptext.

CALL METHOD dg_dyndoc_id->add_text

EXPORTING

text         = lv_text

sap_style    = cl_dd_area=>heading

sap_fontsize = cl_dd_area=>large

sap_color    = cl_dd_area=>list_heading_int. 

* Add new-line

CALL METHOD dg_dyndoc_id->new_line.
* Header2

WRITE s_budat-low TO g_fdate.

WRITE s_budat-high TO g_tdate.

lv_name = c_header2.

PERFORM f_read_text USING lv_name

CHANGING lv_reptext.

REPLACE '&G_FDATE&' INTO lv_reptext WITH g_fdate.

REPLACE '&G_TDATE&' INTO lv_reptext WITH g_tdate.

lv_text = lv_reptext.

CALL METHOD dg_dyndoc_id->add_gap.

CALL METHOD g_dyndoc_id->add_text

EXPORTING

text         = lv_text

sap_emphasis = cl_dd_area=>heading.

* Add new-line

CALL METHOD dg_dyndoc_id->new_line.
* Header3

lv_name = c_header3.

PERFORM f_read_text USING lv_name

CHANGING lv_reptext.

lv_text = lv_reptext.

CALL METHOD dg_dyndoc_id->add_gap.

CALL METHOD g_dyndoc_id->add_text

EXPORTING

text         = lv_text

sap_emphasis = cl_dd_area=>heading.

* Add new-line

CALL METHOD dg_dyndoc_id->new_line.

* Header4

lv_name = c_header4.

PERFORM f_read_text USING lv_name

CHANGING lv_reptext.

lv_text = lv_reptext.

li_t093b[] = i_t093b[].

SORT li_t093b BY waers.

DELETE ADJACENT DUPLICATES FROM li_t093b COMPARING waers.

LOOP AT li_t093b INTO lst_t093b.

CONCATENATE lv_text lst_t093b-waers INTO lv_text

SEPARATED BY space.

ENDLOOP.

CALL METHOD dg_dyndoc_id->add_gap.

CALL METHOD g_dyndoc_id->add_text

EXPORTING

text         = lv_text

sap_emphasis = cl_dd_area=>heading.
* Display output

PERFORM f_display.

ENDFORM.                    " EVENT_TOP_OF_PAGE
*&---------------------------------------------------------------------*

*&      Subroutine F_DISPLAY

*&---------------------------------------------------------------------*

*       Display data

*----------------------------------------------------------------------*

FORM f_display.

* Creating html control

IF g_html_cntrl IS INITIAL.

CREATE OBJECT g_html_cntrl

EXPORTING

parent = g_parent_top.

ENDIF.

CALL METHOD g_dyndoc_id->merge_document.

g_dyndoc_id->html_control = g_html_cntrl.

* Display document

CALL METHOD g_dyndoc_id->display_document

EXPORTING

reuse_control      = abap_true

parent             = g_parent_top

EXCEPTIONS

html_display_error = 1.

IF sy-subrc NE 0.

*    Error in displaying top-of-page

MESSAGE i023.

LEAVE LIST-PROCESSING.

ENDIF.

ENDFORM.                    " display

19.5.Write the following code before the method set_table_for_first_display is called: :

SET HANDLER ref_aplic->handle_top_of_page FOR grid_r.

CALL METHOD g_dyndoc_id->initialize_document

EXPORTING

background_color = cl_dd_area=>col_textarea.

* Processing events

CALL METHOD g_grid->list_processing_events

EXPORTING

i_event_name = 'TOP_OF_PAGE'

i_dyndoc_id  = g_dyndoc_id.

A little assistance with data declaration:

g_dyndoc_id        TYPE REF TO cl_dd_document,   "Object ref for document header

g_splitter         TYPE REF TO cl_gui_splitter_container, "Object ref for splitter

g_parent_grid      TYPE REF TO cl_gui_container,   "Object ref for grid container

g_parent_top       TYPE REF TO cl_gui_container,   "Object ref for top container

g_html_cntrl       TYPE REF TO cl_gui_html_viewer, "Object ref for html control

g_custom_container TYPE REF TO cl_gui_custom_container, "Object ref for custom container

i_fcat             TYPE lvc_t_fcat.         "Field catalog

We would now like to speak with you.

Do you believe that we have addressed the majority of the ALV points?

In either scenario, kindly write a brief remark below, regardless of whether you believe we have covered everything.

Related Posts

ALV Report on SAP HANA: Opportunities and Challenges, Part XX, ABAP for SAP HANA

Everyone has been swept away by the SAP HANA Storm. According to a SAP study, over a thousand clients were already using S/4HANA. Therefore, all practitioners working on SAP would…

Apply SAP is Google Map API Free to find the latitude and longitude of any location.

SAP is Google Map API Free never ceases to amaze us. Even after working for over ten years, a new project or client may have particular requirements that call for…

Leave a Reply

Your email address will not be published. Required fields are marked *

You Missed

A Comprehensive Guide to Interactive SAP ALV

  • By Varad
  • October 22, 2024
  • 2 views

ALV Report on SAP HANA: Opportunities and Challenges, Part XX, ABAP for SAP HANA

  • By Varad
  • October 21, 2024
  • 2 views

Apply SAP is Google Map API Free to find the latitude and longitude of any location.

  • By Varad
  • October 18, 2024
  • 3 views

Part XI: ABAP on SAP HANA. Are Open SQL and Native SQL rivals?

  • By Varad
  • October 17, 2024
  • 2 views

SAP Fiori App: An ABAPer’s The Beginning

  • By Varad
  • October 15, 2024
  • 2 views
SAP Fiori App: An ABAPer’s The Beginning

Part I of the SAP Fiori Tutorial: System Validation, setup, and Configuration

  • By Varad
  • October 14, 2024
  • 3 views
Part I of the SAP Fiori Tutorial: System Validation, setup, and Configuration