Example Usage of REUSE_ALV_HIERSEQ_LIST_DISPLAY:
The function module REUSE_ALV_HIERSEQ_LIST_DISPLAY is used to display hierarchical-sequential (hier-seq) ALV lists in SAP ABAP. This type of ALV report is useful when you need to display data that has a hierarchical structure, such as parent-child relationships.
Below is an example that demonstrates how to use the REUSE_ALV_HIERSEQ_LIST_DISPLAY function module to display a hierarchical-sequential ALV list report.
This example demonstrates how to implement a hierarchical-sequential ALV report using the REUSE_ALV_HIERSEQ_LIST_DISPLAY function module, showcasing the display of hierarchical data with parent-child relationships.
Code Example:
REPORT z_alv_hierseq_display_example.
TYPE-POOLS: slis.
DATA: gt_parent TYPE TABLE OF zparent_table, " Replace zparent_table with the actual parent table name
gt_child TYPE TABLE OF zchild_table, " Replace zchild_table with the actual child table name
gt_fieldcat_parent TYPE slis_t_fieldcat_alv,
gs_fieldcat_parent TYPE slis_fieldcat_alv,
gt_fieldcat_child TYPE slis_t_fieldcat_alv,
gs_fieldcat_child TYPE slis_fieldcat_alv,
gt_event TYPE slis_t_event,
gs_event TYPE slis_alv_event,
gt_sort TYPE slis_t_sortinfo_alv,
gs_sort TYPE slis_sortinfo_alv,
gs_layout TYPE slis_layout_alv,
gs_keyinfo TYPE slis_keyinfo_alv.
* Fetch data for parent table
SELECT * FROM zparent_table INTO TABLE gt_parent.
* Fetch data for child table
SELECT * FROM zchild_table INTO TABLE gt_child.
* Define field catalog for parent table
gs_fieldcat_parent-fieldname = 'PARENT_ID'.
gs_fieldcat_parent-seltext_m = 'Parent ID'.
APPEND gs_fieldcat_parent TO gt_fieldcat_parent.
gs_fieldcat_parent-fieldname = 'PARENT_NAME'.
gs_fieldcat_parent-seltext_m = 'Parent Name'.
APPEND gs_fieldcat_parent TO gt_fieldcat_parent.
* Define field catalog for child table
gs_fieldcat_child-fieldname = 'CHILD_ID'.
gs_fieldcat_child-seltext_m = 'Child ID'.
APPEND gs_fieldcat_child TO gt_fieldcat_child.
gs_fieldcat_child-fieldname = 'PARENT_ID'.
gs_fieldcat_child-seltext_m = 'Parent ID'.
APPEND gs_fieldcat_child TO gt_fieldcat_child.
gs_fieldcat_child-fieldname = 'CHILD_NAME'.
gs_fieldcat_child-seltext_m = 'Child Name'.
APPEND gs_fieldcat_child TO gt_fieldcat_child.
* Define sort information
gs_sort-fieldname = 'PARENT_ID'.
gs_sort-up = 'X'.
gs_sort-down = ' '.
APPEND gs_sort TO gt_sort.
* Define key information
gs_keyinfo-keyfield = 'PARENT_ID'.
APPEND gs_keyinfo TO gs_layout-keyinfo.
* Set up events for user commands
gs_event-name = 'USER_COMMAND'.
gs_event-form = 'USER_COMMAND'.
APPEND gs_event TO gt_event.
* 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 hierarchical-sequential display function module
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = gs_layout
it_events = gt_event
it_fieldcat = gt_fieldcat_parent
i_tabname_header = 'GT_PARENT'
i_tabname_item = 'GT_CHILD'
it_fieldcat_item = gt_fieldcat_child
TABLES
t_outtab_header = gt_parent
t_outtab_item = gt_child
t_sort = gt_sort
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'ALV Hierarchical-Sequential Report could not be displayed.'.
ENDIF.
Explanation:
1. Data Retrieval:
- The SELECT statements fetch data from the parent (zparent_table) and child (zchild_table) tables into internal tables gt_parent and gt_child, respectively. Replace zparent_table and zchild_table with the actual table names.
2. Field Catalog Definition:
- The field catalogs (gt_fieldcat_parent and gt_fieldcat_child) are defined to specify the columns and their properties for the parent and child tables. Each field's metadata is appended to the respective field catalogs.
3. Sort Information:
- The sort information (gt_sort) is defined to specify the sorting criteria for the report. In this example, the report is sorted by PARENT_ID.
4. Key Information:
- The key information (gs_layout-keyinfo) is defined to specify the key fields for the hierarchical relationship. In this example, PARENT_ID is the key field.
5. 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.
6. 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).
7. ALV Display:
- The REUSE_ALV_HIERSEQ_LIST_DISPLAY function module is called with the necessary parameters, including the data tables (t_outtab_header and t_outtab_item), field catalogs (it_fieldcat and it_fieldcat_item), layout (is_layout), and events (it_events).
8. Error Handling:
- The program checks the return code (sy-subrc) to ensure the ALV report is displayed correctly.