In the series of the SALV Model Table display, today we will see how to apply colors to the ALV Grid. In this regards, we will see how to apply colors to the Specific Cell, Specific Row or Specific Column. You can find all the Previous discussion at Tutorials > SALV Table Display.
Color plays a very important role in the formatting. It will help us to generate the a rich list which can help users to notice the exceptional data - for example, negative salary to employee or Low material availabilty.
At most colors can be applied to three levels:
- Perticular Cell: To apply color at perticular cell, we need to add the details about the field and color in COLOR table at each record. Than we have to set this Color Table in the object of the COLUMNS which contains the information about all our information (CL_SALV_COLUMNS_TABLE).
- Entire Row: To apply color to Entire row, we have to do the same thing as the applying the color to Perticular Cell, but we will not specify the FIELDNAME. This way system will understand that it has to apply the color to entire row.
- Entire Column: To apply color to Entire column, we have to get the specific Column from the COLUMNS object and than we need to set the Color Property of the Specific Column and we are done.
In this example I need to change the output table sturcture as compared to previous discussion in this series, so I will provide the entire code which generate the output which has all three scenario. For test purpose, We will apply: Red color to Sales Doc Type in 3rd Row, Green Color to 5th row, Yellow color to Created on Column.
UML Diagram for this application is like:
Code snippet to generate the ALV with Colors using the SALV model.
Code Snippet to Generate ALV with Colors |
*&---------------------------------------------------------------------* *& This code snippet will show how to use the CL_SALV_TABLE to *& generate the ALV and COLORs for Column, Row and Specific Cell *&---------------------------------------------------------------------* REPORT ztest_oo_alv_color. * *----------------------------------------------------------------------* * CLASS lcl_report DEFINITION *----------------------------------------------------------------------* CLASS lcl_report DEFINITION. * PUBLIC SECTION. * * Final output table TYPES: BEGIN OF ty_vbak, vbeln TYPE vbak-vbeln, erdat TYPE erdat, auart TYPE auart, kunnr TYPE kunnr, t_color TYPE lvc_t_scol, END OF ty_vbak. TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak. * DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak. * * ALV reference DATA: o_alv TYPE REF TO cl_salv_table. * METHODS: * data selection get_data, * * Generating output generate_output. * *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$* * * In this section we will define the private methods which can * be implemented to set the properties of the ALV and can be * called in the * PRIVATE SECTION. METHODS: set_pf_status CHANGING co_alv TYPE REF TO cl_salv_table. * METHODS: set_colors CHANGING co_alv TYPE REF TO cl_salv_table ct_vbak TYPE ty_t_vbak. * *$*$*.....CODE_ADD_1 - End....................................1..*$*$* * ENDCLASS. "lcl_report DEFINITION * * START-OF-SELECTION. DATA: lo_report TYPE REF TO lcl_report. * CREATE OBJECT lo_report. * lo_report->get_data( ). * lo_report->generate_output( ). * *----------------------------------------------------------------------* * CLASS lcl_report IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_report IMPLEMENTATION. * METHOD get_data. * data selection SELECT vbeln erdat auart kunnr INTO CORRESPONDING FIELDS OF TABLE t_vbak FROM vbak UP TO 20 ROWS. * ENDMETHOD. "get_data * *....................................................................... METHOD generate_output. * New ALV instance * We are calling the static Factory method which will give back * the ALV object reference. * * exception class DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = t_vbak ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. * *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$* * * In this area we will call the methods which will set the * different properties to the ALV * * Set default PF status CALL METHOD set_pf_status CHANGING co_alv = o_alv. * * Set the colors to ALV display CALL METHOD set_colors CHANGING co_alv = o_alv ct_vbak = t_vbak. *$*$*.....CODE_ADD_2 - End....................................2..*$*$* * * * Displaying the ALV * Here we will call the DISPLAY method to get the output on the screen o_alv->display( ). * ENDMETHOD. "generate_output * *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$* * * In this area we will implement the methods which are defined in * the class definition * * METHOD set_pf_status. * DATA: lo_functions TYPE REF TO cl_salv_functions_list. * lo_functions = co_alv->get_functions( ). lo_functions->set_default( abap_true ). * ENDMETHOD. "set_pf_status * METHOD set_colors. * *.....Color for COLUMN..... DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table, lo_col_tab TYPE REF TO cl_salv_column_table. DATA: ls_color TYPE lvc_s_colo. " Colors strucutre * * get Columns object lo_cols_tab = co_alv->get_columns( ). * INCLUDE . * * Get ERDAT column & set the yellow Color fot it TRY. lo_col_tab ?= lo_cols_tab->get_column( 'ERDAT' ). ls_color-col = col_total. lo_col_tab->set_color( ls_color ). CATCH cx_salv_not_found. ENDTRY. * *.......Color for Specific Cell & Rows................. * Applying color on the 3rd Row and Column AUART * Applying color on the Entire 5th Row * DATA: lt_s_color TYPE lvc_t_scol, ls_s_color TYPE lvc_s_scol, la_vbak LIKE LINE OF ct_vbak, l_count TYPE i. * LOOP AT ct_vbak INTO la_vbak. l_count = l_count + 1. CASE l_count. * Apply RED color to the AUART Cell of the 3rd Column WHEN 3. ls_s_color-fname = 'AUART'. ls_s_color-color-col = col_negative. ls_s_color-color-int = 0. ls_s_color-color-inv = 0. APPEND ls_s_color TO lt_s_color. CLEAR ls_s_color. * * Apply GREEN color to the entire row # 5 * For entire row, we don't pass the Fieldname WHEN 5. ls_s_color-color-col = col_positive. ls_s_color-color-int = 0. ls_s_color-color-inv = 0. APPEND ls_s_color TO lt_s_color. CLEAR ls_s_color. ENDCASE. * Modify that data back to the output table la_vbak-t_color = lt_s_color. MODIFY ct_vbak FROM la_vbak. CLEAR la_vbak. CLEAR lt_s_color. ENDLOOP. * * We will set this COLOR table field name of the internal table to * COLUMNS tab reference for the specific colors TRY. lo_cols_tab->set_color_column( 'T_COLOR' ). CATCH cx_salv_data_error. "#EC NO_HANDLER ENDTRY. * ENDMETHOD. "set_colors * * * *$*$*.....CODE_ADD_3 - End....................................3..*$*$* ENDCLASS. "lcl_report IMPLEMENTATION
|
Without Colors it will look like:
With Colors it will look like:
Thx, coloring a cell was very useful to me! ^^
Hi, I have come across to problem in a part where you use INCLUDE. statement ("include.", "TYPE ...", "METHODS.", "STRUCTURE ...", or "include IF FOUND." expected after "INCLUDE".) and right after this, there's another problem with col_total (Field "COL_TOTAL" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement. ).