Vous êtes sur la page 1sur 5

8/31/2016 AddcellcolorinALVusingCL_SALV_TABLESAPFiori,SAPHANA,SAPUI5,SAPNetweaverGatewayTutorials,InterviewQuestions|SAPLearners

Add cell color in ALV using CL_SALV_TABLE


By uiravikumar@gmail.com

In this tutorial, we will learn how to color a particular cell in ALV using the factory class
CL_SALV_TABLE.In earlier tutorial we have added colors to row in ALV, click here to
know how. This is similar to coloring row. Lets see how we can do it.

To apply cell color,

Create a new column of Type LVC_T_SCOL in your output internal table.


Set the above column as Color Column in ALV by using the method SET_COLUMN_COLOR( ).
Add the color data to the color column based on your requirement.

Create a program in SE38 and copy the below code.

1 *&*
2 *&AddcolortocellinALVusingfactoryclass*
3 *&*
4 *&www.saplearners.com*
5 *&*
6 REPORTzsl_oop_alv_row_color.
7
8 **
9 *CLASSlcl_sflightDEFINITION
10 **
11 *
12 **
13 CLASSlcl_sflightDEFINITION.
14
15 PUBLICSECTION.
16 TYPES:BEGINOFlty_sflight,
17 carrid TYPEs_carr_id,
18 connid TYPEs_conn_id,
19 fldate TYPEs_date,
20 priceTYPEs_price,
21 currency TYPEs_currcode,
22 planetypeTYPEs_planetye,
23 seatsmax TYPEs_seatsmax,
24 seatsocc TYPEs_seatsocc,
http://www.saplearners.com/addcellcoloralvusingcl_salv_table/ 1/5
8/31/2016 AddcellcolorinALVusingCL_SALV_TABLESAPFiori,SAPHANA,SAPUI5,SAPNetweaverGatewayTutorials,InterviewQuestions|SAPLearners

25 colorTYPElvc_t_scol,"<<ColorColumn
26 ENDOFlty_sflight.
27
28 METHODS:get_sflight_data,
29 get_alv_instance,
30 set_color,
31 display.
32
33 DATA:lo_alvTYPEREFTOcl_salv_table,
34 gt_sflightTYPESTANDARDTABLEOFlty_sflight.
35
36 ENDCLASS."lcl_sflightDEFINITION
37
38 **
39 *CLASSlcl_sflightIMPLEMENTATION
40 **
41 *
42 **
43 CLASSlcl_sflightIMPLEMENTATION.
44
45 *GetSFLIGHTdata
46 METHODget_sflight_data.
47 SELECTcarridconnidfldatepricecurrencyplanetypeseatsmax
48 seatsoccINTOCORRESPONDINGFIELDSOFTABLEme>gt_sflight
49 FROMsflight
50 WHEREcarrid='AA'.
51 ENDMETHOD."get_sflight_data
52
53 *GetALVinstance
54 METHODget_alv_instance.
55 TRY.
56 CALLMETHODcl_salv_table=>factory
57 IMPORTING
58 r_salv_table=lo_alv
59 CHANGING
60 t_table=gt_sflight.
61 CATCHcx_salv_msg.
62 ENDTRY.
63
64 ENDMETHOD."get_alv_instance
65
66 *DisplayALV
67 METHODdisplay.
68 CALLMETHODlo_alv>display.
69 ENDMETHOD."display
70 METHODset_color.
71 *Setcolortoaparticularrowbasedonyourcondition.

http://www.saplearners.com/addcellcoloralvusingcl_salv_table/ 2/5
8/31/2016 AddcellcolorinALVusingCL_SALV_TABLESAPFiori,SAPHANA,SAPUI5,SAPNetweaverGatewayTutorials,InterviewQuestions|SAPLearners

72 FIELDSYMBOLS:<lwa_sflight>TYPElty_sflight.
73 DATA:ls_colorTYPElvc_s_scol.
74
75 LOOPATgt_sflightASSIGNING<lwa_sflight>.
76 IF<lwa_sflight>seatsocc>370.
77 ls_colorfname='SEATSOCC'."<<Specifythecolumnname
78 ls_colorcolorcol=5.
79 ls_colorcolorint=0.
80 ls_colorcolorinv=0.
81 APPENDls_colorTO<lwa_sflight>color.
82 ENDIF.
83 ENDLOOP.
84 ENDMETHOD."set_color
85 ENDCLASS."lcl_sflightIMPLEMENTATION
86
87
88 STARTOFSELECTION.
89 DATA:lo_cl_sflightTYPEREFTOlcl_sflight,
90 lo_columnsTYPEREFTOcl_salv_columns_table.
91
92
93 CREATEOBJECTlo_cl_sflight.
94
95 *GettheDataforALVreport
96 lo_cl_sflight>get_sflight_data().
97
98 *GetALVinstance
99 lo_cl_sflight>get_alv_instance().
100
101
102 *ToapplyCOLORtoaROWinALV*
103 *//1.GetListofcolumns
104 CALLMETHODlo_cl_sflight>lo_alv>get_columns
105 RECEIVING
106 value=lo_columns.
107
108 *//2.SettheColorColumntotheALV
109 TRY.
110 CALLMETHODlo_columns>set_color_column
111 EXPORTING
112 value='COLOR'.
113 CATCHcx_salv_data_error.
114 ENDTRY.
115
116 *//3.Addcolortoparticularrow
117 lo_cl_sflight>set_color().
118

http://www.saplearners.com/addcellcoloralvusingcl_salv_table/ 3/5
8/31/2016 AddcellcolorinALVusingCL_SALV_TABLESAPFiori,SAPHANA,SAPUI5,SAPNetweaverGatewayTutorials,InterviewQuestions|SAPLearners

119 *DisplayALVreport
120 lo_cl_sflight>display().

cellcolor_salv.abap hosted with by GitHub view raw

Output Before:

Output after adding color to cells:

uiravikumar@gmail.com

http://www.saplearners.com/addcellcoloralvusingcl_salv_table/ 4/5
8/31/2016 AddcellcolorinALVusingCL_SALV_TABLESAPFiori,SAPHANA,SAPUI5,SAPNetweaverGatewayTutorials,InterviewQuestions|SAPLearners

http://www.saplearners.com/addcellcoloralvusingcl_salv_table/ 5/5