In this example I’ll show how to add a column which will hold a user defined icon to the ALV grid. The core is:
- Add new field to the output table
gt_data
(this will hold the icon information) - Modify the field catalog
gt_fieldcat
so it’s updated with info about our new field and set this new field as icon - Read and prepare data for output (read data from DB and update the icon field based on your criteria)
- Display or refresh our ALV grid (
g_grid->set_table_for_first_display
) using the modified field catalog
1. Data definition
Here we create the output table with field ICON – this will be used to store the icon name.
TYPES: BEGIN OF ty_data.
INCLUDE STRUCTURE sflight.
TYPES: icon(4) TYPE c, " field to hold the icon
END OF ty_data.
DATA: gt_data TYPE TABLE OF ty_data,
g_grid TYPE REF TO cl_gui_alv_grid,
gs_layout TYPE lvc_s_layo,
gt_fieldcat TYPE lvc_t_fcat.
2. Field catalog
Now we prepare field catalog for data structure we’re going to use and update it with the new ICON field
FORM get_fieldcat .
FIELD-SYMBOLS: <fs_fcat> TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = gt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
CHECK sy-subrc = 0.
APPEND INITIAL LINE TO gt_fieldcat ASSIGNING <fs_fcat>.
<fs_fcat>-fieldname = 'ICON'.
<fs_fcat>-icon = 'X'.
<fs_fcat>-outputlen = 3.
ENDFORM.
3. Read data
In our example we would like to add “warning” icon when some of the travel classes is full (see the line <fs_data>-icon = '@AH@'.
)
FORM read_data.
DATA:
l_color TYPE c.
FIELD-SYMBOLS:
<fs_data> TYPE ty_data.
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM sflight
UP TO c_max_rows ROWS.
LOOP AT gt_data ASSIGNING <fs_data>.
IF <fs_data>-seatsocc = <fs_data>-seatsmax OR
<fs_data>-seatsocc_b = <fs_data>-seatsmax_b OR
<fs_data>-seatsocc_f = <fs_data>-seatsmax_f.
" Some class is full
<fs_data>-icon = '@AH@'. " Warning Icon
ENDIF.
ENDLOOP.
ENDFORM.
Together with field catalog we modified the ALV grid layout to display lines in “zebra” style
FORM get_layout .
gs_layout-zebra = 'X'.
ENDFORM.
4. Display data
Now we display our data with new field containing a warning icon in case some of the travel classes is full
FORM display_grid.
PERFORM get_layout.
PERFORM get_fieldcat.
CREATE OBJECT g_grid
EXPORTING
i_parent = cl_gui_container=>default_screen.
CALL METHOD g_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
is_layout = gs_layout
CHANGING
it_outtab = gt_data
it_fieldcatalog = gt_fieldcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.