In this blog, we will explore how to create an ALV (ABAP List Viewer) grid in SAP using the CL_SALV_TABLE class, where each row displays both icons and symbols dynamically. This approach enhances the user interface by visually representing data using intuitive graphical elements, such as status icons or symbols. Let's break down the implementation of this functionality.
Overview of the Code
The provided code is implemented in a custom class (LCL_SALV_TAB) designed to display flight data with corresponding status, icons, and symbols using ALV. We will go through the code to explain each component and how the functionality is achieved.
CLASS lcl_salv_tab DEFINITION.
PUBLIC SECTION.
CLASS-METHODS : main IMPORTING
i_list TYPE Xfeld
i_grid TYPE Xfeld
i_alv_tb TYPE c,
fetch_records,
get_alv_instance IMPORTING
i_list TYPE Xfeld
i_grid TYPE Xfeld
i_alv_tb TYPE c,
display_alv.
PRIVATE SECTION.
TYPE-POOLS : icon, sym.
TYPES : BEGIN OF ty_flight,
status TYPE c LENGTH 1,
icon TYPE ICON_d,
* ” char – 4
symbol TYPE ICON_d.
* !” char – 4
INCLUDE TYPE sflight.
TYPES : END OF ty_flight.
CLASS-DATA : lt TYPE TABLE OF ty_flight,
ls TYPE ty_flight,
lo_salv_tab TYPE REF TO cl_salv_table,
lo_func TYPE REF TO cl_salv_functions_list,
lo_cols TYPE REF TO cl_salv_columns_table,
lo_col TYPE REF TO cl_salv_column,
lo_col_ICON TYPE REF TO cl_salv_column,
lo_ICON TYPE REF TO cl_salv_column_table,
lo_col_sym TYPE REF TO cl_salv_column,
lo_SYMBOL TYPE REF TO cl_salv_column_table,
rem_seat TYPE i,
lt_ICON TYPE TABLE OF icon,
ls_ICON TYPE icon.
ENDCLASS.
CLASS lcl_salv_tab IMPLEMENTATION.
METHOD main.
fetch_records( ).
get_alv_instance( EXPORTING i_list = i_list
i_grid = i_grid
i_alv_tb = i_alv_tb ).
display_alv( ).
ENDMETHOD.
METHOD fetch_records.
DATA indX TYPE sy-tabiX.
DATA line TYPE i.
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE lt UP TO 40 ROWS.
line = lines( lt ).
SELECT * FROM icon INTO TABLE lt_ICON UP TO line ROWS.
LOOP AT lt INTO ls.
indX = sy-tabiX.
rem_seat = ls-seatsmaX_b - ls-seatsocc_b.
IF rem_seat = 0.
ls-status = 1 .
ELSEIF rem_seat LE 10.
ls-status = 2.
ELSE.
ls-status = 3.
ENDIF.
ls-symbol = sym_caution.
READ TABLE lt_ICON INTO ls_ICON INDEX indX .
IF sy-subrc = 0.
ls-icon = ls_ICON-id.
ENDIF.
IF indX <> 0.
MODIFY lt FROM ls INDEX indX TRANSPORTING status icon symbol.
ENDIF.
CLEAR ls.
ENDLOOP.
ENDMETHOD.
METHOD get_alv_instance.
DATA : flag.
IF i_list = 'X' OR i_grid = 'X'.
IF i_list = 'X'.
flag = 'X'.
ELSE.
flag = ' '.
ENDIF.
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = flag
IMPORTING
r_salv_table = lo_salv_tab
CHANGING
t_table = lt.
* IF i_alv_tb = abap_true.
**Begin- Displaying toolbar on alv **
CALL METHOD lo_salv_tab->get_functions
* ” Get the instance of “alv toolbal button
RECEIVING
value = lo_func.
CALL METHOD lo_func->set_default
* ” pass 'TRUE' to display ” toolbar on alv
EXPORTING
value = if_salv_c_bool_sap=>true.
**End- Displaying toolbar on alv**
* endif.
** Begin – Hides paritular column of the table in the list or grid**
CALL METHOD lo_salv_tab->get_columns
* ” get all columns of the “table
RECEIVING
value = lo_cols.
TRY.
CALL METHOD lo_cols->get_column
* ” get reference to particular “column
EXPORTING
columnname = 'MANDT'
RECEIVING
value = lo_col.
CALL METHOD lo_col->set_technical
* ” set technical – true hides “the column on UI
EXPORTING
value = if_salv_c_bool_sap=>true.
CATCH cX_salv_not_found .
ENDTRY.
** End – Hides paritular column of the table in the list or grid**
** Begin – Set STATUS field as traffic ICON **
TRY.
CALL METHOD lo_cols->set_eXception_column
EXPORTING
value = 'STATUS'.
CATCH cX_salv_data_error .
ENDTRY.
**End – Set STATUS field as traffic ICON **
**Begin – Set ICON for the column ICON**
TRY.
CALL METHOD lo_cols->get_column
* ” get reference to particular “column
EXPORTING
columnname = 'ICON'
RECEIVING
value = lo_col_ICON.
lo_ICON ?= lo_col_ICON.
CALL METHOD lo_ICON->set_ICON
EXPORTING
value = if_salv_c_bool_sap=>true.
CALL METHOD lo_ICON->set_long_teXt
EXPORTING
value = 'ICON'.
CATCH cX_salv_not_found .
ENDTRY.
**End – Set ICON for the column ICON**
**Start- SYMBOL**
TRY.
CALL METHOD lo_cols->get_column
* ” get reference to particular “column
EXPORTING
columnname = 'SYMBOL'
RECEIVING
value = lo_col_sym.
lo_SYMBOL ?= lo_col_sym.
CALL METHOD lo_SYMBOL->set_SYMBOL
EXPORTING
value = if_salv_c_bool_sap=>true.
CALL METHOD lo_SYMBOL->set_long_teXt
EXPORTING
value = 'SYMBOL'.
* ” Column name set
CATCH cX_salv_not_found .
ENDTRY.
**End SYMBOL**
CATCH cX_salv_msg .
ENDTRY.
ENDIF.
ENDMETHOD.
METHOD display_alv.
CALL METHOD lo_salv_tab->display.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS : list RADIOBUTTON GROUP g1,
grid RADIOBUTTON GROUP g1,
alv_tool AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b1.
CALL METHOD lcl_salv_tab=>main
EXPORTING
i_list = list
i_grid = grid
i_alv_tb = alv_tool.
CONCLUSION
In this above ABAP report demonstrates how to fetch flight data from the SFLIGHT table and present it dynamically using the ALV (ABAP List Viewer) with enhanced features such as icon representation and customized column handling. The program efficiently categorizes flights based on available seats, assigns corresponding status values, and displays them with appropriate icons.
Key features of the program include:
This report is ideal for scenarios where users need to view flight information with visual indicators of seat availability, making it a useful tool for monitoring flight capacity and managing reservations. By leveraging the ALV framework, it enhances the user experience with clear, easy-to-understand visuals, and provides flexibility in how the data is presented.