Example of Using REUSE_ALV_EVENTS_GET:
The function module REUSE_ALV_EVENTS_GET is used to retrieve a list of possible ALV events that can be handled during ALV report processing. This function module is useful when you want to define custom event handling for an ALV report.
This example demonstrates how to use REUSE_ALV_EVENTS_GET to retrieve ALV events and set up custom event handling in an ALV report such as a double-click on a row.
Define the ABAP Program:
The program fetches data from the SFLIGHT table, generates a field catalog, retrieves ALV events, and sets up event handling for a double-click event.
Code Example:
REPORT z_alv_events_example.
DATA: lt_flight_data TYPE TABLE OF sflight,
lt_fieldcat TYPE lvc_t_fcat,
lt_events TYPE slis_t_event,
ls_event TYPE slis_alv_event,
lv_exit TYPE char1.
* Fetch data from the SFLIGHT table
SELECT * INTO TABLE lt_flight_data FROM sflight.
* Generate the field catalog
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inheriting_parameters = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
WRITE: / 'Error generating field catalog'.
EXIT.
ENDIF.
* Get ALV events
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
et_events = lt_events.
* Find the event for double-click
READ TABLE lt_events INTO ls_event WITH KEY name = 'DOUBLE_CLICK'.
IF sy-subrc = 0.
ls_event-form = 'USER_COMMAND'.
MODIFY lt_events FROM ls_event INDEX sy-tabix.
ENDIF.
* Display the ALV report with event handling
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = lt_fieldcat
it_events = lt_events
TABLES
t_outtab = lt_flight_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: / 'Error displaying ALV grid'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
FORM user_command USING ucomm LIKE sy-ucomm
selfield TYPE slis_selfield.
DATA: ls_flight TYPE sflight.
CASE ucomm.
WHEN '&IC1'. " Double-click event
READ TABLE lt_flight_data INTO ls_flight INDEX selfield-tabindex.
IF sy-subrc = 0.
WRITE: / 'Double-clicked on flight: ', ls_flight-carrid, ls_flight-connid.
ENDIF.
ENDCASE.
ENDFORM.
Explanation:
1. Data Declaration:
- lt_flight_data is an internal table of type SFLIGHT to store the flight data.
- lt_fieldcat is an internal table of type LVC_T_FCAT to store the field catalog.
- lt_events is an internal table of type SLIS_T_EVENT to store the ALV events.
- ls_event is a structure of type SLIS_ALV_EVENT to store individual event information.
2. Fetching Data:
- The SELECT statement retrieves all records from the SFLIGHT table and stores them in lt_flight_data.
3. Generating Field Catalog:
- The function module REUSE_ALV_FIELDCATALOG_MERGE generates the field catalog based on the structure of SFLIGHT.
4. Retrieving ALV Events:
- The function module REUSE_ALV_EVENTS_GET retrieves the list of possible ALV events and stores them in lt_events.
5. Setting Up Event Handling:
- The program searches for the double-click event (DOUBLE_CLICK) and assigns the form USER_COMMAND to handle this event.
6. Displaying ALV Report:
- The function module REUSE_ALV_GRID_DISPLAY is called to display the ALV report with the specified field catalog and events.
7. Event Handling Form (USER_COMMAND):
- This form handles the double-click event. When a user double-clicks on a row, it reads the corresponding flight data and displays a message with the flight details.
Usage:
- Save and activate the ABAP program (z_alv_events_example).
- Execute the program. The ALV grid will display the data from the SFLIGHT table.
- Double-click on any row in the ALV grid. The program will handle the double-click event and display the flight details of the selected row.