SAP: SALV Grid 單元格顏色

samrv發表於2024-06-14

1、SAP: SALV Grid 單元格顏色

2、詳細程式程式碼

*&---------------------------------------------------------------------*
*& Report  Z17_43
*&
*&---------------------------------------------------------------------*
*& SALV單元格顏色
*&
*&---------------------------------------------------------------------*

REPORT Z17_43.

INCLUDE <color>.

*& 定義內表結構體
TYPES: BEGIN OF g_type_itab.
        INCLUDE type sflight.
TYPES: t_color type lvc_t_scol,   "單元格顏色 型別
end of g_type_itab. " g_type_itab 結構體

DATA: gt_itab TYPE TABLE OF g_type_itab.
DATA: gs_itab TYPE g_type_itab,
      gr_table TYPE REF TO cl_salv_table.


*&
SELECT * FROM sflight into CORRESPONDING FIELDS OF TABLE
  gt_itab up to 25 rows.


FIELD-SYMBOLS: <ls_outtab>  type g_type_itab.
DATA: lt_color type lvc_t_scol,
      ls_color type lvc_s_scol.

LOOP AT gt_itab ASSIGNING <ls_outtab>.
  CLEAR: ls_color, lt_color.

  " 佔據的座位數量大於 90時, 紅色 。"
  IF <ls_outtab>-seatsocc >= 90 .
    ls_color-fname = 'SEATSOCC'.
    ls_color-color-col = col_negative.
    ls_color-color-int = 0 .
    ls_color-color-inv = 0.
    APPEND ls_color to lt_color.
    "小於50 常規色
  elseIF <ls_outtab>-seatsocc >= 50 .
    ls_color-fname = 'SEATSOCC'.
    ls_color-color-col = col_normal.
    ls_color-color-int = 0 .
    ls_color-color-inv = 0.
    APPEND ls_color to lt_color.
    " 其他情況 為綠色 "
  else .
    ls_color-fname = 'SEATSOCC'.
    ls_color-color-col = col_positive.
    ls_color-color-int = 0 .
    ls_color-color-inv = 0.
    APPEND ls_color to lt_color.
  ENDIF.

  <ls_outtab>-t_color = lt_color.
ENDLOOP.

*&
cl_salv_table=>factory(
   IMPORTING
  r_salv_table = gr_table
  CHANGING
    t_table = gt_itab
).

*&
DATA: lr_columns TYPE REF TO cl_salv_columns_table,
      lr_column TYPE REF TO cl_salv_column_table.

lr_columns = gr_table->get_columns( ).
lr_columns->set_color_column( 'T_COLOR' ).

gr_table->display( ).
*&

  

3、效果圖

相關文章