Example of Using REUSE_ALV_EVENTS_GET:
The function module REUSE_ALV_COMMENTARY_WRITE is used in SAP ABAP to add custom headers, footers, and other commentary lines to an ALV report. This can help provide context or additional information about the data being displayed.
Below is an example that demonstrates how to use the REUSE_ALV_COMMENTARY_WRITE function module to add a header and footer to an ALV report.
Code Example:
REPORT z_alv_commentary_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_list_top TYPE slis_t_listheader,
gs_list_top TYPE slis_listheader.
* 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.
* Define header lines
gs_list_top-typ = 'H'.
gs_list_top-info = 'Flight Information Report'.
APPEND gs_list_top TO gt_list_top.
gs_list_top-typ = 'S'.
gs_list_top-info = 'Generated on: ' && sy-datum.
APPEND gs_list_top TO gt_list_top.
* Define footer lines
DATA: gt_list_bottom TYPE slis_t_listheader,
gs_list_bottom TYPE slis_listheader.
gs_list_bottom-typ = 'S'.
gs_list_bottom-info = 'End of Report'.
APPEND gs_list_bottom TO gt_list_bottom.
* Call the ALV display function module with commentary
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = gt_fieldcat
i_callback_program = sy-repid
TABLES
t_outtab = gt_data
t_top_of_page = gt_list_top
t_end_of_page = gt_list_bottom
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
WRITE: 'ALV Report could not be displayed.'.
ENDIF.
Explanation:
1. Data Retrieval:
- The SELECT statement fetches 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. Header Lines:
- Header lines are defined in gt_list_top using the slis_t_listheader structure. The typ field is set to 'H' for headers and 'S' for subheaders.
4. Footer Lines:
- Footer lines are defined in gt_list_bottom similarly to the header lines.
5. ALV Display:
- The REUSE_ALV_GRID_DISPLAY function module is called with the necessary parameters including the data table (t_outtab), field catalog (it_fieldcat), and the commentary lines (t_top_of_page for headers and t_end_of_page for footers).
6. Error Handling:
- The program checks the return code (sy-subrc) to ensure the ALV report is displayed correctly.