Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

OO ALV colour cells

Former Member
0 Kudos

Hello,

I have implemented a code in which i wanto to colour some cells in the ALV according to a condition.

My code is.

LOOP AT GT_ALVTABLE INTO GW_ALVTABLE.
        IF GW_ALVTABLE-APLFZ <> '0'.
          LV_S_COLOR-FNAME = 'NAME1'.
          LV_S_COLOR-COLOR-COL =  6. "red
          LV_S_COLOR-COLOR-INT =  1.
          LV_S_COLOR-COLOR-INV =  0.
          APPEND LV_S_COLOR TO LV_T_COLOR.
          GW_ALVTABLE-T_COLOR = LV_T_COLOR.
          MODIFY GT_ALVTABLE FROM GW_ALVTABLE.
        ENDIF.

      ENDLOOP.

      TRY.
          GCL_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' ).
        CATCH CX_SALV_DATA_ERROR.
      ENDTRY.

    GCL_TABLE->DISPLAY( ).

T_COLOR is a column in the GT_ALVTABLE

TYPE LVC_T_SCOL

When i execute it it doesnt colour those cells.

Have i forget something or it cant be done by this way ?

Thx in advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi,

You can refer to any of the following link:

Thank You.

Regards,

Dhanalakshmi L

18 REPLIES 18

Former Member
0 Kudos

Hi,

Check this link.

Thanks.

former_member555112
Active Contributor
0 Kudos

Hi,

Have you passed the name 'T_COLOR' of the column in the field CTAB_FNAME of the Layout of the ALV grid object?

The field CTAB_FNAME of the Layout of the ALV grid object should contain the column name of your internal table which hold the complex color coding table.

Regards,

Ankur Parab

0 Kudos

@ Ankur Parab

I think no. How do i pass it ?

I made this according to [this|http://help-abap.blogspot.com/2008/10/salv-table-9-apply-colors.html] example

0 Kudos

Hi,

Check the code given SreeMohan in his reply.

Regards,

Ankur Parab

Former Member
0 Kudos

Former Member
0 Kudos

Hi,

Add a field in your internal table(itab) used for displaying data, let's say cellcolors of type lvc_t_scol.

types: begin of x_flight.

include structure sflight.

types: cellcolors TYPE lvc_t_scol,

end of x_flight.

set field ctab_fname in layout parameter

layout-ctab_fname = 'CELLCOLORS'.

while populating your output table, specify color code in CELLCOLORS

loop at itab into wa.

if wa-field1 = 'X'.

wa_col-fname = 'PRICE'. "Cell to be coloured

wa_col-color-col = '6'. "Color

wa_col-color-int = '0'. "intensified

append wa_col to wa-cellcolors.

endif.

wa-field2 = 'ABC'.

append wa to itab.

endloop.

just pass the layout and the internal table in OOPS method SET_TABLE_FoR_FIRST_DISPLAY

Thanks & regards,

ShreeMohan

0 Kudos

I use the

CL_SALV_TABLE=>FACTORY( IMPORTING R_SALV_TABLE = GCL_TABLE
                              CHANGING T_TABLE = GT_ALVTABLE ).

to create the 2D table and not the set for first display.

And I cant found the ctab_fname anywhere.

Where is it ?

0 Kudos

Hi,

do mention the column name on which you want to display the color.

try .
        lr_column ?= lr_columns->get_column( 'MENGE' ).
        ls_color-col = col_negative.
        ls_color-int = 0.
        ls_color-inv = 0.
      catch cx_salv_not_found into oref.
        text = oref->get_text( ).
      cleanup.
    endtry.
    lr_column->set_color( ls_color ).
    try.
        lr_columns->set_color_column( 'T_COLOR' ).
      catch cx_salv_data_error into oref.
        text = oref->get_text( ).
      cleanup.
    endtry.

Regards

Sunil

0 Kudos

If i do this all cells in the column will be coloured!

Former Member
0 Kudos

HI,

I think after LOOP ... ENDLOOP, you are setting the color. Before setting the color, you have to call that column and set the color details. The below code may help you ..

-


First Part----


LOOP AT GT_ALVTABLE INTO GW_ALVTABLE.

IF GW_ALVTABLE-APLFZ '0'.

LV_S_COLOR-FNAME = 'NAME1'.

LV_S_COLOR-COLOR-COL = 6. "red

LV_S_COLOR-COLOR-INT = 1.

LV_S_COLOR-COLOR-INV = 0.

APPEND LV_S_COLOR TO LV_T_COLOR.

GW_ALVTABLE-T_COLOR = LV_T_COLOR.

MODIFY GT_ALVTABLE FROM GW_ALVTABLE.

ENDIF.

ENDLOOP.

__________________Second Part______________________

TRY .

gr_p_column ?= gr_p_columns->get_column( 'NAME1 ).

ls_color-col = 6.

ls_color-int = 0.

ls_color-inv = 0.

CATCH cx_salv_not_found INTO lv_oref.

lv_text = lv_oref->get_text( ).

CLEANUP.

ENDTRY.

gr_p_column->set_color( ls_color ).

_________________Third Part____________________________

TRY.

GCL_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' ).

CATCH CX_SALV_DATA_ERROR.

ENDTRY.

GCL_TABLE->DISPLAY( ).

_________________________________________________________

The 2nd section code I have pasted from my own code. Just refer this section to match yours(declaration of ref objects).

Former Member
0 Kudos

Hi check this below program it will help you.

[http://help-abap.blogspot.com/search/label/SALV%20Tutorial]

it uses cl_salv_table.

Regards

Sunil

Edited by: sunil kumar on Jul 30, 2009 1:50 PM

Former Member
0 Kudos

Hello

Check this link:

kesavadas_thekkillath
Active Contributor
0 Kudos

link:[https://wiki.sdn.sap.com/wiki/display/Snippets/ColoringaRowandColumninALV+%28OOPS%29]

Former Member
0 Kudos

hi,

You can refer to any of the following link:

Thank You.

Regards,

Dhanalakshmi L

0 Kudos

Thx for the links. I searched a lot before i post.

I will ppreciate if someone could help me with the code...

0 Kudos

Hi,

Understand the code as follows:-

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.

* create the grid object
  CREATE OBJECT gc_container
    EXPORTING
      container_name              = 'CC_CONTAINER'
    EXCEPTIONS
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5
      OTHERS                      = 6.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  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.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* 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

Edited by: Ankur Parab on Jul 30, 2009 4:14 PM

0 Kudos

*continuing the code

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.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDMODULE.

I hope you understand the code and teh concept.

Regards,

Ankur Parab

Former Member
0 Kudos

hi,

When you loop at GT_ALVTABLE if you can just maintain an index and when you finally Modify the internal table

use that index to color the cells.

Eg:

MODIFY GT_ALVTABLE INDEX l_index FROM GW_ALVTABLE

with this even if it does not work you will be able to tell whether

it is reaching till the correct cell for colouring.

Hope this works