07-30-2009 8:25 AM
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.
07-30-2009 9:53 AM
07-30-2009 8:31 AM
07-30-2009 8:37 AM
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
07-30-2009 8:47 AM
@ 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
07-30-2009 8:51 AM
Hi,
Check the code given SreeMohan in his reply.
Regards,
Ankur Parab
07-30-2009 8:38 AM
07-30-2009 8:43 AM
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
07-30-2009 9:07 AM
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 ?
07-30-2009 9:25 AM
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
07-30-2009 9:33 AM
07-30-2009 8:51 AM
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).
07-30-2009 8:55 AM
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
07-30-2009 9:13 AM
07-30-2009 9:36 AM
link:[https://wiki.sdn.sap.com/wiki/display/Snippets/ColoringaRowandColumninALV+%28OOPS%29]
07-30-2009 9:53 AM
07-30-2009 10:05 AM
Thx for the links. I searched a lot before i post.
I will ppreciate if someone could help me with the code...
07-30-2009 11:41 AM
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
07-30-2009 11:45 AM
*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
07-30-2009 11:49 AM
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