How to display “Traffic Light” through ALV class CL_SALV_TABLE in SAP. This is useful while displaying custom status reports, message logs etc, as an easy visual representation.
The code:
REPORT ztest_sb_alv1.TYPES: BEGIN OF x_output,
icon TYPE c LENGTH 4,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
currency TYPE s_currcode,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
seatocper TYPE p LENGTH 3 DECIMALS 2,
END OF x_output,
x_output_tt TYPE STANDARD TABLE OF x_output WITH NON-UNIQUE DEFAULT KEY.
DATA it_output TYPE x_output_tt.
PARAMETERS: p_carrid TYPE s_carr_id OBLIGATORY.
AT SELECTION-SCREEN OUTPUT.
DATA lt_sel_texts TYPE STANDARD TABLE OF rsseltexts.
APPEND VALUE #( name = 'P_CARRID' kind = 'P' text = 'Carrier ID' ) TO lt_sel_texts.
CALL FUNCTION 'SELECTION_TEXTS_MODIFY'
EXPORTING
program = sy-repid
TABLES
seltexts = lt_sel_texts.
START-OF-SELECTION.
SELECT carrid,
connid,
fldate,
price,
currency,
planetype,
seatsmax,
seatsocc
FROM sflight
INTO TABLE @DATA(li_sflight)
WHERE carrid = @p_carrid.
IF sy-subrc <> 0.
MESSAGE i001(00) WITH 'No data found!!!'.
LEAVE LIST-PROCESSING.
ENDIF.
END-OF-SELECTION.
it_output = VALUE #(
FOR lwa_flight IN li_sflight
(
carrid = lwa_flight-carrid
connid = lwa_flight-connid
fldate = lwa_flight-fldate
price = lwa_flight-price
currency = lwa_flight-currency
planetype = lwa_flight-planetype
seatsmax = lwa_flight-seatsmax
seatsocc = lwa_flight-seatsocc
seatocper = round( val = ( ( lwa_flight-seatsocc / lwa_flight-seatsmax ) * 100 ) dec = 2 )
icon = COND #(
WHEN round( val = ( ( lwa_flight-seatsocc / lwa_flight-seatsmax ) * 100 ) dec = 0 ) < 30
THEN icon_green_light
WHEN round( val = ( ( lwa_flight-seatsocc / lwa_flight-seatsmax ) * 100 ) dec = 0 ) BETWEEN 30 AND 80
THEN icon_yellow_light
WHEN round( val = ( ( lwa_flight-seatsocc / lwa_flight-seatsmax ) * 100 ) dec = 0 ) > 80
THEN icon_red_light )
)
).
IF it_output IS NOT INITIAL.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lr_alv)
CHANGING
t_table = it_output ).
CATCH cx_salv_msg.
ENDTRY.
IF lr_alv IS BOUND.
lr_alv->get_display_settings( )->set_list_header( 'Demo Traffic Light' ).
lr_alv->get_functions( )->set_all( abap_true ).
lr_alv->get_columns( )->set_optimize( ).
lr_alv->get_columns( )->get_column( 'ICON' )->set_short_text( 'Status' ).
lr_alv->get_columns( )->get_column( 'ICON' )->set_medium_text( 'Status' ).
lr_alv->get_columns( )->get_column( 'ICON' )->set_long_text( 'Status' ).
lr_alv->get_columns( )->get_column( 'SEATOCPER' )->set_short_text( 'Occ %' ).
lr_alv->get_columns( )->get_column( 'SEATOCPER' )->set_medium_text( 'Occupency %' ).
lr_alv->get_columns( )->get_column( 'SEATOCPER' )->set_long_text( 'Occupency %' ).
lr_alv->display( ).
ENDIF.
ELSE.
MESSAGE i001(00) WITH 'No data found!!!'.
LEAVE LIST-PROCESSING.
ENDIF.
No comments:
Post a Comment