Example Usage of REUSE_ALV_BLOCK_LIST_APPEND:
The function module REUSE_ALV_BLOCK_LIST_APPEND is used to append multiple ALV blocks into a single list report. This is useful when you need to display multiple related datasets in separate sections within the same report.
This example demonstrates how to implement a block list ALV report using the REUSE_ALV_BLOCK_LIST_APPEND function module, showcasing the display of multiple datasets in separate sections within the same report.
Below is an example demonstrating how to use the REUSE_ALV_BLOCK_LIST_APPEND function module to display a block list ALV report.
Code Example:
REPORT z_alv_block_list_append_example.
TYPE-POOLS: slis.
DATA: gt_spfli TYPE TABLE OF spfli,
gt_sflight TYPE TABLE OF sflight,
gt_fieldcat_spfli TYPE slis_t_fieldcat_alv,
gs_fieldcat_spfli TYPE slis_fieldcat_alv,
gt_fieldcat_sflight TYPE slis_t_fieldcat_alv,
gs_fieldcat_sflight TYPE slis_fieldcat_alv.
* Fetch data from the SPFLI table (flight schedule)
SELECT * FROM spfli INTO TABLE gt_spfli.
* Fetch data from the SFLIGHT table (flight details)
SELECT * FROM sflight INTO TABLE gt_sflight.
* Define field catalog for SPFLI table
gs_fieldcat_spfli-fieldname = 'CARRID'.
gs_fieldcat_spfli-seltext_m = 'Carrier ID'.
APPEND gs_fieldcat_spfli TO gt_fieldcat_spfli.
gs_fieldcat_spfli-fieldname = 'CONNID'.
gs_fieldcat_spfli-seltext_m = 'Connection ID'.
APPEND gs_fieldcat_spfli TO gt_fieldcat_spfli.
gs_fieldcat_spfli-fieldname = 'CITYFROM'.
gs_fieldcat_spfli-seltext_m = 'Departure City'.
APPEND gs_fieldcat_spfli TO gt_fieldcat_spfli.
gs_fieldcat_spfli-fieldname = 'CITYTO'.
gs_fieldcat_spfli-seltext_m = 'Arrival City'.
APPEND gs_fieldcat_spfli TO gt_fieldcat_spfli.
* Define field catalog for SFLIGHT table
gs_fieldcat_sflight-fieldname = 'CARRID'.
gs_fieldcat_sflight-seltext_m = 'Carrier ID'.
APPEND gs_fieldcat_sflight TO gt_fieldcat_sflight.
gs_fieldcat_sflight-fieldname = 'CONNID'.
gs_fieldcat_sflight-seltext_m = 'Connection ID'.
APPEND gs_fieldcat_sflight TO gt_fieldcat_sflight.
gs_fieldcat_sflight-fieldname = 'FLDATE'.
gs_fieldcat_sflight-seltext_m = 'Flight Date'.
APPEND gs_fieldcat_sflight TO gt_fieldcat_sflight.
gs_fieldcat_sflight-fieldname = 'PRICE'.
gs_fieldcat_sflight-seltext_m = 'Price'.
APPEND gs_fieldcat_sflight TO gt_fieldcat_sflight.
* First block for SPFLI table
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = 'USER_COMMAND'
is_layout = ' '
it_fieldcat = gt_fieldcat_spfli
TABLES
t_outtab = gt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Error in ALV Block List Initialization'.
RETURN.
ENDIF.
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
i_tabname = 'GT_SPFLI'
it_fieldcat = gt_fieldcat_spfli
i_default = 'X'
is_layout = ' '
i_save = 'A'
TABLES
t_outtab = gt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Error in ALV Block List Append'.
RETURN.
ENDIF.
* Second block for SFLIGHT table
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
i_tabname = 'GT_SFLIGHT'
it_fieldcat = gt_fieldcat_sflight
i_default = 'X'
is_layout = ' '
i_save = 'A'
TABLES
t_outtab = gt_sflight
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Error in ALV Block List Append'.
RETURN.
ENDIF.
* Display the ALV block list
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Error in ALV Block List Display'.
ENDIF.
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.
Explanation:
1. Data Retrieval:
- The SELECT statements fetch data from the SPFLI and SFLIGHT tables into internal tables gt_spfli and gt_sflight, respectively.
2. Field Catalog Definition:
- Field catalogs (gt_fieldcat_spfli and gt_fieldcat_sflight) are defined for each table to specify the columns and their properties for the ALV report. Each field's metadata is appended to the respective field catalog.
3. ALV Block Initialization:
- The REUSE_ALV_BLOCK_LIST_INIT function module is called to initialize the ALV block list. It sets up the basic structure and configuration for the report.
4. ALV Block Append for SPFLI:
- The REUSE_ALV_BLOCK_LIST_APPEND function module is called to append the first block, which displays data from the SPFLI table. The parameters include the table name, field catalog, and layout.
5. ALV Block Append for SFLIGHT:
- The REUSE_ALV_BLOCK_LIST_APPEND function module is called again to append the second block, which displays data from the SFLIGHT table.
6. ALV Block List Display:
- The REUSE_ALV_BLOCK_LIST_DISPLAY function module is called to display the complete ALV block list report.
7. Event Handling:
- A form user_command is defined to handle user commands. In this example, it displays a message when a row is selected (triggered by a double-click or function key).
8. Error Handling:
- The program checks the return code (sy-subrc) after each function module call to ensure the ALV report is displayed correctly.