SAP ABAP - Set Individual Cell Color ALV Grid
*&---------------------------------------------------------------------*
*& Report ZALVCOLOR
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALVCOLOR.
TYPES: BEGIN OF Y_T_ITAB,
MATNR TYPE MATNR,
MAKTX TYPE MAKTX,
COLORTAB TYPE LVC_T_SCOL,
END OF Y_T_ITAB.
DATA : ITAB TYPE STANDARD TABLE OF Y_T_ITAB.
DATA : COLOR_TAB TYPE STANDARD TABLE OF LVC_S_SCOL.
DATA : IT_FCAT TYPE STANDARD TABLE OF LVC_S_FCAT.
DATA : WA_ITAB TYPE Y_T_ITAB.
FIELD-SYMBOLS: <F_WA_ITAB> TYPE Y_T_ITAB.
DATA : WA_FCAT TYPE LVC_S_FCAT,
WA_LAYO TYPE LVC_S_LAYO,
WA_COLOR TYPE LVC_S_SCOL.
DATA : GC_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
GC_GRID TYPE REF TO CL_GUI_ALV_GRID.
SELECT-OPTIONS : S_MATNR FOR WA_ITAB-MATNR.
SELECT MATNR MAKTX FROM MAKT
INTO CORRESPONDING FIELDS OF TABLE ITAB
WHERE MATNR IN S_MATNR
AND SPRAS = SY-LANGU.
IF SY-SUBRC EQ 0.
SORT ITAB BY MATNR.
ENDIF.
CALL SCREEN '0100'.
*----------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'GG'.
SET TITLEBAR 'TT'.
* create the grid object
CREATE OBJECT GC_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
OTHERS = 6.
CREATE OBJECT GC_GRID
EXPORTING
I_PARENT = GC_CONTAINER
EXCEPTIONS
ERROR_CNTL_CREATE = 1
ERROR_CNTL_INIT = 2
ERROR_CNTL_LINK = 3
ERROR_DP_CREATE = 4
OTHERS = 5.
* create fcat
WA_FCAT-FIELDNAME = 'MATNR'.
WA_FCAT-REF_FIELD = 'MATNR'.
WA_FCAT-REF_TABLE = 'MAKT'.
APPEND WA_FCAT TO IT_FCAT.
WA_FCAT-FIELDNAME = 'MAKTX'.
WA_FCAT-REF_FIELD = 'MAKTX'.
WA_FCAT-REF_TABLE = 'MAKT'.
APPEND WA_FCAT TO IT_FCAT.
*add colors to the cells
* LOOPing just to add different colors to different records
LOOP AT ITAB ASSIGNING <F_WA_ITAB>.
REFRESH : COLOR_TAB[].
CASE SY-TABIX.
WHEN 1.
WA_COLOR-FNAME = 'MATNR'.
WA_COLOR-COLOR-COL = 6. " red color
APPEND WA_COLOR TO COLOR_TAB.
WA_COLOR-FNAME = 'MAKTX'.
WA_COLOR-COLOR-COL = 5. " green color
APPEND WA_COLOR TO COLOR_TAB.
WHEN OTHERS.
WA_COLOR-FNAME = 'MATNR'.
WA_COLOR-COLOR-COL = 3. "yellow
APPEND WA_COLOR TO COLOR_TAB.
WA_COLOR-FNAME = 'MAKTX'.
WA_COLOR-COLOR-COL = 7. "violet
APPEND WA_COLOR TO COLOR_TAB.
ENDCASE.
INSERT LINES OF COLOR_TAB INTO TABLE <F_WA_ITAB>-COLORTAB.
ENDLOOP.
* pass the column name of the internal table
* which contains the color sequence to the layout of the grid
WA_LAYO-CTAB_FNAME = 'COLORTAB'.
* display the grid
CALL METHOD GC_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = WA_LAYO
CHANGING
IT_OUTTAB = ITAB[]
IT_FIELDCATALOG = IT_FCAT[]
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
*& Report ZALVCOLOR
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALVCOLOR.
TYPES: BEGIN OF Y_T_ITAB,
MATNR TYPE MATNR,
MAKTX TYPE MAKTX,
COLORTAB TYPE LVC_T_SCOL,
END OF Y_T_ITAB.
DATA : ITAB TYPE STANDARD TABLE OF Y_T_ITAB.
DATA : COLOR_TAB TYPE STANDARD TABLE OF LVC_S_SCOL.
DATA : IT_FCAT TYPE STANDARD TABLE OF LVC_S_FCAT.
DATA : WA_ITAB TYPE Y_T_ITAB.
FIELD-SYMBOLS: <F_WA_ITAB> TYPE Y_T_ITAB.
DATA : WA_FCAT TYPE LVC_S_FCAT,
WA_LAYO TYPE LVC_S_LAYO,
WA_COLOR TYPE LVC_S_SCOL.
DATA : GC_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
GC_GRID TYPE REF TO CL_GUI_ALV_GRID.
SELECT-OPTIONS : S_MATNR FOR WA_ITAB-MATNR.
SELECT MATNR MAKTX FROM MAKT
INTO CORRESPONDING FIELDS OF TABLE ITAB
WHERE MATNR IN S_MATNR
AND SPRAS = SY-LANGU.
IF SY-SUBRC EQ 0.
SORT ITAB BY MATNR.
ENDIF.
CALL SCREEN '0100'.
*----------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'GG'.
SET TITLEBAR 'TT'.
* create the grid object
CREATE OBJECT GC_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
OTHERS = 6.
CREATE OBJECT GC_GRID
EXPORTING
I_PARENT = GC_CONTAINER
EXCEPTIONS
ERROR_CNTL_CREATE = 1
ERROR_CNTL_INIT = 2
ERROR_CNTL_LINK = 3
ERROR_DP_CREATE = 4
OTHERS = 5.
* create fcat
WA_FCAT-FIELDNAME = 'MATNR'.
WA_FCAT-REF_FIELD = 'MATNR'.
WA_FCAT-REF_TABLE = 'MAKT'.
APPEND WA_FCAT TO IT_FCAT.
WA_FCAT-FIELDNAME = 'MAKTX'.
WA_FCAT-REF_FIELD = 'MAKTX'.
WA_FCAT-REF_TABLE = 'MAKT'.
APPEND WA_FCAT TO IT_FCAT.
*add colors to the cells
* LOOPing just to add different colors to different records
LOOP AT ITAB ASSIGNING <F_WA_ITAB>.
REFRESH : COLOR_TAB[].
CASE SY-TABIX.
WHEN 1.
WA_COLOR-FNAME = 'MATNR'.
WA_COLOR-COLOR-COL = 6. " red color
APPEND WA_COLOR TO COLOR_TAB.
WA_COLOR-FNAME = 'MAKTX'.
WA_COLOR-COLOR-COL = 5. " green color
APPEND WA_COLOR TO COLOR_TAB.
WHEN OTHERS.
WA_COLOR-FNAME = 'MATNR'.
WA_COLOR-COLOR-COL = 3. "yellow
APPEND WA_COLOR TO COLOR_TAB.
WA_COLOR-FNAME = 'MAKTX'.
WA_COLOR-COLOR-COL = 7. "violet
APPEND WA_COLOR TO COLOR_TAB.
ENDCASE.
INSERT LINES OF COLOR_TAB INTO TABLE <F_WA_ITAB>-COLORTAB.
ENDLOOP.
* pass the column name of the internal table
* which contains the color sequence to the layout of the grid
WA_LAYO-CTAB_FNAME = 'COLORTAB'.
* display the grid
CALL METHOD GC_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = WA_LAYO
CHANGING
IT_OUTTAB = ITAB[]
IT_FIELDCATALOG = IT_FCAT[]
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
Comments
Post a Comment