Implementing and Customizing ALV Reports with SAP ABAP Objects
Inspecting CL_GUI_ALV_GRID in Transaction SE24
When working with Object-Oriented ALV (OOALV), the central class is CL_GUI_ALV_GRID. Understanding its definition in SE24 is crucial before implementation.
Class Attributes
Attributes in SE24 correspond to DATA (instance) or CLASS-DATA (static) in local classes. Pay attention to the Visibility (Public, Protected, Private) as it dictates accessibility. The Associated Type column indicates the data type; types containing _S_ typically represent structures, while _T_ indicates table types.
Methods
Instance methods (created with METHODS) require an object instance to be called, while static methods (CLASS-METHODS) can be called on the class itself. To analyze a method's logic, double-click its name in SE24 to view the implementation. Note that methods inherited from interfaces may appear with a wavy line prefix.
Events
Event are pivotal for interactivity. In SE24, you can see which parameters an event passes. A standard implicit parameter in ALV events is SENDER, which references the object that raised the event.
Aliases
Aliases provide shorthand names for interface methods, simplifying calls by avoiding the interface_name~method_name syntax.
Building an OOALV Report: Step-by-Step
The proces involves creating a container, instantiating the grid, and passing data.
1. Container and Grid Instantiation
OOALV requires a parent container. While CL_GUI_CONTAINER is the abstract base, CL_GUI_CUSTOM_CONTAINER is commonly used when a screen control is drawn in the Screen Painter.
The constructor of CL_GUI_CUSTOM_CONTAINER requires CONTAINER_NAME, which must match the name of the Custom Control defined on your screen (e.g., CUSTOM_01).
DATA: lo_container TYPE REF TO cl_gui_custom_container,
lo_grid TYPE REF TO cl_gui_alv_grid.
CREATE OBJECT lo_container
EXPORTING
container_name = 'CUSTOM_01'.
CREATE OBJECT lo_grid
EXPORTING
i_parent = lo_container.
2. Defining Data and Field Catalog
Define a local structure for your output data and an internal table.
TYPES: BEGIN OF ty_employee,
id TYPE string,
name TYPE string,
dept TYPE string,
salary TYPE i,
END OF ty_employee.
DATA: gt_employees TYPE TABLE OF ty_employee,
gt_fcat TYPE lvc_t_fcat.
Build the field catalog. You can use a macro or populate the LVC_S_FCAT structure manually.
FORM build_catalog .
DATA: ls_fcat TYPE lvc_s_fcat.
ls_fcat-fieldname = 'ID'.
ls_fcat-coltext = 'Employee ID'.
APPEND ls_fcat TO gt_fcat.
ls_fcat-fieldname = 'NAME'.
ls_fcat-coltext = 'Full Name'.
APPEND ls_fcat TO gt_fcat.
ls_fcat-fieldname = 'DEPT'.
ls_fcat-coltext = 'Department'.
APPEND ls_fcat TO gt_fcat.
ls_fcat-fieldname = 'SALARY'.
ls_fcat-coltext = 'Salary'.
ls_fcat-no_out = 'X'. "Hide initially
APPEND ls_fcat TO gt_fcat.
ENDFORM.
3. Displaying the ALV
Call SET_TABLE_FOR_FIRST_DISPLAY. Pass the layout structure, field catalog, and output table.
FORM display_alv .
DATA: ls_layout TYPE lvc_s_layo.
ls_layout-zebra = 'X'.
ls_layout-cwidth_opt = 'X'.
CALL METHOD lo_grid->set_table_for_first_display
EXPORTING
is_layout = ls_layout
CHANGING
it_outtab = gt_employees
it_fieldcatalog = gt_fcat.
ENDFORM.
4. Screen Flow Logic
Ensure the logic is triggered in the PBO (Process Before Output) of your screen.
PROCESS BEFORE OUTPUT.
MODULE status_set.
MODULE data_fetch.
MODULE alv_render.
PROCESS AFTER INPUT.
MODULE user_action.
Refreshing Data
To update the display without reinitializing the grid, use REFRESH_TABLE_DISPLAY.
FORM refresh_grid .
DATA: ls_stable TYPE lvc_s_stbl.
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD lo_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
ENDFORM.
Handling Events (Interaction)
OOALV becomes powerful when reacting to user actions like toolbar clicks or data changes.
Defining the Event Handler
Create a local class to handle events raised by CL_GUI_ALV_GRID.
CLASS lcl_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object,
on_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
CLASS lcl_handler IMPLEMENTATION.
METHOD on_toolbar.
" Add a custom button
DATA: ls_btn TYPE stb_button.
ls_btn-function = 'RELOAD'.
ls_btn-icon = icon_refresh.
ls_btn-quickinfo = 'Reload Data'.
APPEND ls_btn TO e_object->mt_toolbar.
ENDMETHOD.
METHOD on_user_command.
CASE e_ucomm.
WHEN 'RELOAD'.
PERFORM fetch_new_data.
PERFORM refresh_grid.
ENDCASE.
ENDMETHOD.
ENDCLASS.
Registering the Handler
Instantiate the handler and link it to the grid instance using SET HANDLER.
DATA: go_handler TYPE REF TO lcl_handler.
FORM register_events .
CREATE OBJECT go_handler.
SET HANDLER go_handler->on_toolbar FOR lo_grid.
SET HANDLER go_handler->on_user_command FOR lo_grid.
ENDFORM.
Editing Cells and Styles
To make specific cells editable while keeping others read-only, use the STYLE table in your data structure.
- Add a field
t_styleof typeLVC_T_STYLto your data structure. - Map this field in the layout:
ls_layout-stylefname = 'T_STYLE'. - Populate the style table for rows where editing is allowed.
FORM set_editable_styles .
DATA: ls_style TYPE lvc_s_styl.
LOOP AT gt_employees ASSIGNING FIELD-SYMBOL(<fs_emp>).
IF <fs_emp>-dept = 'IT'. " Only IT department rows editable
CLEAR ls_style.
ls_style-fieldname = 'SALARY'.
ls_style-style = cl_gui_alv_grid=>mc_style_enabled.
INSERT ls_style INTO TABLE <fs_emp>-t_style.
ENDIF.
ENDLOOP.
ENDFORM.
Excluding Standard Buttons
To hide specific toolbar buttons (like Insert Row or Delete Row), populate an UI_FUNCTIONS table with the constants from CL_GUI_ALV_GRID.
FORM exclude_buttons .
DATA: lt_exclude TYPE ui_functions.
APPEND cl_gui_alv_grid=>mc_fc_loc_delete_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO lt_exclude.
CALL METHOD lo_grid->set_table_for_first_display
EXPORTING
it_toolbar_excluding = lt_exclude
" ... other params
ENDFORM.