Example Usage of REUSE_ALV_LIST_DISPLAY:
The function module REUSE_ALV_LIST_DISPLAY is used to display data in a list format using the ALV (ABAP List Viewer) grid. This function module offers a wide range of features such as sorting, filtering, and subtotalling of data.
This example demonstrates a basic implementation of the REUSE_ALV_LIST_DISPLAY function module, showcasing how to display an ALV list report with custom columns and event handling for user interactions.
Below is an example that demonstrates how to use the REUSE_ALV_LIST_DISPLAY function module to display a simple ALV list report.
Code Example:
REPORT z_alv_list_display_example.
TYPE-POOLS: slis.
DATA: gt_data TYPE TABLE OF spfli,
gt_fieldcat TYPE slis_t_fieldcat_alv,
gs_fieldcat TYPE slis_fieldcat_alv,
gt_events TYPE slis_t_event,
gs_event TYPE slis_alv_event.
* Fetch data from the SPFLI table
SELECT * FROM spfli INTO TABLE gt_data.
* Define field catalog
gs_fieldcat-fieldname = 'CARRID'.
gs_fieldcat-seltext_m = 'Carrier ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CONNID'.
gs_fieldcat-seltext_m = 'Connection ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CITYFROM'.
gs_fieldcat-seltext_m = 'Departure City'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CITYTO'.
gs_fieldcat-seltext_m = 'Arrival City'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'FLTIME'.
gs_fieldcat-seltext_m = 'Flight Time'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'DISTANCE'.
gs_fieldcat-seltext_m = 'Distance'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'DISTID'.
gs_fieldcat-seltext_m = 'Distance ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'PLANETYPE'.
gs_fieldcat-seltext_m = 'Plane Type'.
APPEND gs_fieldcat TO gt_fieldcat.
* Set up events for user commands
gs_event-name = 'USER_COMMAND'.
gs_event-form = 'USER_COMMAND'.
APPEND gs_event TO gt_events.
* Create a form to handle user commands
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN '&IC1'.
MESSAGE i001(zalv) WITH 'You selected' rs_selfield-value.
ENDCASE.
ENDFORM.
* Call the ALV display function module
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = gt_fieldcat
it_events = gt_events
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'ALV Report could not be displayed.'.
ENDIF.
Explanation:
1. Data Retrieval:
- The SELECT statement retrieves data from the SPFLI table into an internal table gt_data.
2. Field Catalog Definition:
- The field catalog (gt_fieldcat) is defined to specify the columns and their properties for the ALV report. Each field's metadata is appended to gt_fieldcat.
3. Event Handling:
- Events are set up to handle user commands. Here, a USER_COMMAND event is defined, which calls the USER_COMMAND form when triggered.
4. Form Definition:
- A form user_command is defined to handle the USER_COMMAND event. In this example, it displays a message when a row is selected (triggered by a double-click or function key).
5. ALV Display:
- The REUSE_ALV_LIST_DISPLAY function module is called with the necessary parameters, including the data table (t_outtab), field catalog (it_fieldcat), and events (it_events).
6. Error Handling:
- The program checks the return code (sy-subrc) to ensure the ALV report is displayed correctly.