Application Development and Automation 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: 
Read only

oops alv

Former Member
0 Likes
876

hi abapers

i m having a query i want to give colors to my rows in alvs

how can i do this i m using alv oops.

thx in advance

8 REPLIES 8
Read only

Former Member
0 Likes
850

Hi

welcome to SDN

see the sample code and do accordingly

Example:

REPORT sapmz_hf_alv_grid .

  • Type pool for icons - used in the toolbar

TYPE-POOLS: icon.

TABLES: zsflight.

  • To allow the declaration of o_event_receiver before the

  • lcl_event_receiver class is defined, decale it as deferred in the

  • start of the program

CLASS lcl_event_receiver DEFINITION DEFERRED.

*----


  • G L O B A L I N T E R N A L T A B L E S

*----


*DATA: gi_sflight TYPE STANDARD TABLE OF sflight.

  • To include a traffic light and/or color a line the structure of the

  • table must include fields for the traffic light and/or the color

TYPES: BEGIN OF st_sflight.

INCLUDE STRUCTURE zsflight.

  • Field for traffic light

TYPES: traffic_light TYPE c.

  • Field for line color

types: line_color(4) type c.

TYPES: END OF st_sflight.

TYPES: tt_sflight TYPE STANDARD TABLE OF st_sflight.

DATA: gi_sflight TYPE tt_sflight.

*----


  • G L O B A L D A T A

*----


DATA: ok_code LIKE sy-ucomm,

  • Work area for internal table

g_wa_sflight TYPE st_sflight,

  • ALV control: Layout structure

gs_layout TYPE lvc_s_layo.

  • Declare reference variables to the ALV grid and the container

DATA:

go_grid TYPE REF TO cl_gui_alv_grid,

go_custom_container TYPE REF TO cl_gui_custom_container,

o_event_receiver TYPE REF TO lcl_event_receiver.

DATA:

  • Work area for screen 200

g_screen200 LIKE zsflight.

  • Data for storing information about selected rows in the grid

DATA:

  • Internal table

gi_index_rows TYPE lvc_t_row,

  • Information about 1 row

g_selected_row LIKE lvc_s_row.

*----


  • C L A S S E S

*----


CLASS lcl_event_receiver DEFINITION.

PUBLIC SECTION.

METHODS:

handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING

e_object e_interactive,

handle_user_command FOR EVENT user_command OF cl_gui_alv_grid

IMPORTING e_ucomm.

ENDCLASS.

----


  • CLASS lcl_event_receiver IMPLEMENTATION

----


CLASS lcl_event_receiver IMPLEMENTATION.

METHOD handle_toolbar.

  • Event handler method for event toolbar.

CONSTANTS:

  • Constants for button type

c_button_normal TYPE i VALUE 0,

c_menu_and_default_button TYPE i VALUE 1,

c_menu TYPE i VALUE 2,

c_separator TYPE i VALUE 3,

c_radio_button TYPE i VALUE 4,

c_checkbox TYPE i VALUE 5,

c_menu_entry TYPE i VALUE 6.

DATA:

ls_toolbar TYPE stb_button.

  • Append seperator to the normal toolbar

CLEAR ls_toolbar.

MOVE c_separator TO ls_toolbar-butn_type..

APPEND ls_toolbar TO e_object->mt_toolbar.

  • Append a new button that to the toolbar. Use E_OBJECT of

  • event toolbar. E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.

  • This class has one attribute MT_TOOLBAR which is of table type

  • TTB_BUTTON. The structure is STB_BUTTON

CLEAR ls_toolbar.

MOVE 'CHANGE' TO ls_toolbar-function.

MOVE icon_change TO ls_toolbar-icon.

MOVE 'Change flight' TO ls_toolbar-quickinfo.

MOVE 'Change' TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.

METHOD handle_user_command.

  • Handle own functions defined in the toolbar

CASE e_ucomm.

WHEN 'CHANGE'.

PERFORM change_flight.

  • LEAVE TO SCREEN 0.

ENDCASE.

ENDMETHOD.

ENDCLASS.

*----


  • S T A R T - O F - S E L E C T I O N.

*----


START-OF-SELECTION.

SET SCREEN '100'.

&----


*& Module USER_COMMAND_0100 INPUT

&----


MODULE user_command_0100 INPUT.

CASE ok_code.

WHEN 'EXIT'.

LEAVE TO SCREEN 0.

ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

&----


*& Module STATUS_0100 OUTPUT

&----


MODULE status_0100 OUTPUT.

DATA:

  • For parameter IS_VARIANT that is sued to set up options for storing

  • the grid layout as a variant in method set_table_for_first_display

l_layout TYPE disvariant,

  • Utillity field

l_lines TYPE i.

  • After returning from screen 200 the line that was selected before

  • going to screen 200, should be selected again. The table gi_index_rows

  • was the output table from the GET_SELECTED_ROWS method in form

  • CHANGE_FLIGHT

DESCRIBE TABLE gi_index_rows LINES l_lines.

IF l_lines > 0.

CALL METHOD go_grid->set_selected_rows

EXPORTING

it_index_rows = gi_index_rows.

CALL METHOD cl_gui_cfw=>flush.

REFRESH gi_index_rows.

ENDIF.

  • Read data and create objects

IF go_custom_container IS INITIAL.

  • Read data from datbase table

PERFORM get_data.

  • Create objects for container and ALV grid

CREATE OBJECT go_custom_container

EXPORTING container_name = 'ALV_CONTAINER'.

CREATE OBJECT go_grid

EXPORTING

i_parent = go_custom_container.

  • Create object for event_receiver class

  • and set handlers

CREATE OBJECT o_event_receiver.

SET HANDLER o_event_receiver->handle_user_command FOR go_grid.

SET HANDLER o_event_receiver->handle_toolbar FOR go_grid.

  • Layout (Variant) for ALV grid

l_layout-report = sy-repid. "Layout fo report

*----


  • Setup the grid layout using a variable of structure lvc_s_layo

*----


  • Set grid title

gs_layout-grid_title = 'Flights'.

  • Selection mode - Single row without buttons

  • (This is the default mode

gs_layout-sel_mode = 'B'.

  • Name of the exception field (Traffic light field) and the color

  • field + set the exception and color field of the table

gs_layout-excp_fname = 'TRAFFIC_LIGHT'.

gs_layout-info_fname = 'LINE_COLOR'.

LOOP AT gi_sflight INTO g_wa_sflight.

IF g_wa_sflight-paymentsum < 100000.

  • Value of traffic light field

g_wa_sflight-traffic_light = '1'.

  • Value of color field:

  • C = Color, 6=Color 1=Intesified on, 0: Inverse display off

g_wa_sflight-line_color = 'C610'.

ELSEIF g_wa_sflight-paymentsum => 100000 AND

g_wa_sflight-paymentsum < 1000000.

g_wa_sflight-traffic_light = '2'.

ELSE.

g_wa_sflight-traffic_light = '3'.

ENDIF.

MODIFY gi_sflight FROM g_wa_sflight.

ENDLOOP.

  • Grid setup for first display

CALL METHOD go_grid->set_table_for_first_display

EXPORTING i_structure_name = 'SFLIGHT'

is_variant = l_layout

i_save = 'A'

is_layout = gs_layout

CHANGING it_outtab = gi_sflight.

*-- End of grid setup -


  • Raise event toolbar to show the modified toolbar

CALL METHOD go_grid->set_toolbar_interactive.

  • Set focus to the grid. This is not necessary in this

  • example as there is only one control on the screen

CALL METHOD cl_gui_control=>set_focus EXPORTING control = go_grid.

ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

&----


*& Module USER_COMMAND_0200 INPUT

&----


MODULE user_command_0200 INPUT.

CASE ok_code.

WHEN 'EXIT200'.

LEAVE TO SCREEN 100.

WHEN'SAVE'.

PERFORM save_changes.

ENDCASE.

ENDMODULE. " USER_COMMAND_0200 INPUT

&----


*& Form get_data

&----


FORM get_data.

  • Read data from table SFLIGHT

SELECT *

FROM zsflight

INTO TABLE gi_sflight.

ENDFORM. " load_data_into_grid

&----


*& Form change_flight

&----


  • Reads the contents of the selected row in the grid, ans transfers

  • the data to screen 200, where it can be changed and saved.

----


FORM change_flight.

DATA:l_lines TYPE i.

REFRESH gi_index_rows.

CLEAR g_selected_row.

  • Read index of selected rows

CALL METHOD go_grid->get_selected_rows

IMPORTING

et_index_rows = gi_index_rows.

  • Check if any row are selected at all. If not

  • table gi_index_rows will be empty

DESCRIBE TABLE gi_index_rows LINES l_lines.

IF l_lines = 0.

CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'

EXPORTING

textline1 = 'You must choose a line'.

EXIT.

ENDIF.

  • Read indexes of selected rows. In this example only one

  • row can be selected as we are using gs_layout-sel_mode = 'B',

  • so it is only ncessary to read the first entry in

  • table gi_index_rows

LOOP AT gi_index_rows INTO g_selected_row.

IF sy-tabix = 1.

READ TABLE gi_sflight INDEX g_selected_row-index INTO g_wa_sflight.

ENDIF.

ENDLOOP.

  • Transfer data from the selected row to screenm 200 and show

  • screen 200

CLEAR g_screen200.

MOVE-CORRESPONDING g_wa_sflight TO g_screen200.

LEAVE TO SCREEN '200'.

ENDFORM. " change_flight

&----


*& Form save_changes

&----


  • Changes made in screen 200 are written to the datbase table

  • zsflight, and to the grid table gi_sflight, and the grid is

  • updated with method refresh_table_display to display the changes

----


FORM save_changes.

DATA: l_traffic_light TYPE c.

  • Update traffic light field

  • Update database table

MODIFY zsflight FROM g_screen200.

  • Update grid table , traffic light field and color field.

  • Note that it is necessary to use structure g_wa_sflight

  • for the update, as the screen structure does not have a

  • traffic light field

MOVE-CORRESPONDING g_screen200 TO g_wa_sflight.

IF g_wa_sflight-paymentsum < 100000.

g_wa_sflight-traffic_light = '1'.

  • C = Color, 6=Color 1=Intesified on, 0: Inverse display off

g_wa_sflight-line_color = 'C610'.

ELSEIF g_wa_sflight-paymentsum => 100000 AND

g_wa_sflight-paymentsum < 1000000.

g_wa_sflight-traffic_light = '2'.

clear g_wa_sflight-line_color.

ELSE.

g_wa_sflight-traffic_light = '3'.

clear g_wa_sflight-line_color.

ENDIF.

MODIFY gi_sflight INDEX g_selected_row-index FROM g_wa_sflight.

  • Refresh grid

CALL METHOD go_grid->refresh_table_display.

CALL METHOD cl_gui_cfw=>flush.

LEAVE TO SCREEN '100'.

ENDFORM. " save_changes

chk this blog

/people/vijaybabu.dudla/blog/2006/07/21/topofpage-in-alv-using-clguialvgrid

Regards

Anji

Read only

uwe_schieferstein
Active Contributor
0 Likes
850

Hello Rao

You will find all necessary information in the excellent tutorial

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e8a1d690-0201-0010-b7ad-d9719a415907">An Easy Reference for ALV Grid Control</a>

Check pages 27 - 29.

Regards

Uwe

Read only

Former Member
0 Likes
850

Hi Mallika,

Check this below link which is a self explanatory tutorial on ALV OOPS.

http://www.abap4.it/download/ALV.pdf

Hope this will resolve your problem.

Read only

Former Member
0 Likes
850

Hi Mallika,

check this link

How can I set the cell color in ALV? http://www.sapfans.com/forums/viewtopic.php?t=52107

Thanks

Read only

Former Member
0 Likes
850

Hi,

1.Add a color table of type LVC_T_SCOL to your output table, as shown in the example below:

2. Select your data and copy it into the output table.

3. Read one row of the output table at a time in the loop.

One row of the color table has three fields.

If field NOKEYCOL is set, you can change the color of key fields.

Assign values to the remaining fields as follows:

&#8722; If you want to color the entire row, assign the corresponding values to the fields of structure COLOR. Field fname does not receive a value in this case.

&#8722; If you want to color specific columns of the row only, you must append one row for each column to the color table. Assign the name of the desired column to field fname, and the corresponding values to the fields of structure COLOR.

eg:

DATA: BEGIN OF GT_OUTTAB OCCURS 0.

INCLUDE STRUCTURE <DDIC-Struktur>.

DATA: CT TYPE LVC_T_SCOL. "Table for colors

DATA: END OF GT_OUTTAB.

select * frm <tablename> into corresponding fields of table gt_outtab.

<b>If a whole row has to be coloured</b>

loop at gt_outtab into wa_outtab.

wa_outtab-CT-COLOR = <value of color>.

modify gt_outtab from wa_outtab index sy-tabix.

endloop.

<b>If you want to color specific columns of the row only</b>

loop at gt_outtab into wa_outtab where <field> = <fieldname>.

wa_outtab-CT-FNAME = <fieldname>.

wa_outtab-CT-COLOR = <value of color>.

modify gt_outtab from wa_outtab index sy-tabix.

endloop.

Hope u understood this.

Regards,

Simy Abraham.

Read only

Former Member
Read only

Former Member
0 Likes
850

Hi

by seeing this program you can understand very easily



tables:
sbook.

data:
w_table type ref to cl_gui_custom_container,
w_grid type ref to cl_gui_alv_grid.

data:
t_catalog type lvc_t_fcat,
fs_catalog like line of t_catalog,
fs_layout type lvc_s_layo.

data begin of fs_sflight.
data check type c.
include structure sflight.
data t_color type lvc_t_scol.

data end of fs_sflight.

data fs_color type lvc_s_scol.

data :
t_sflight like
standard table
of fs_sflight.

select *
from sflight
into corresponding fields of table t_sflight.

call screen 1985.
*---------------------------------------------------------------------*
* MODULE STATUS_1985 OUTPUT
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
module status_1985 output.

set pf-status 'STATUS_1985'.

create object w_table
exporting container_name = 'CONTAINER'.

create object w_grid
exporting i_parent = w_table.

call function 'LVC_FIELDCATALOG_MERGE'
exporting
* I_BUFFER_ACTIVE =
i_structure_name = 'SFLIGHT'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
changing
ct_fieldcat = t_catalog
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.

if sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

fs_layout-grid_title = 'YASH TECHNOLOGIES'.
fs_layout-smalltitle = 'X'.
*fs_layout-NO_HEADERS = 'X'.
fs_layout-no_hgridln = 'X'.
fs_layout-no_vgridln = 'X'.
*FS_LAYOUT-NO_TOOLBAR = 'X'.
*FS_LAYOUT-NO_ROWMARK = 'X'.
fs_layout-zebra = 'X'.
* fs_layout-info_fname = 'CHECK'.
FS_LAYOUT-CTAB_FNAME = 'T_COLOR'.

fs_layout-NO_TOTLINE = 'X'.
loop at t_catalog into fs_catalog.

case fs_catalog-fieldname.
* WHEN 'CHECK'.
* FS_CATALOG-CHECKBOX = 'X'.
** FS_CATALOG-DO_SUM = 'X'.
** FS_CATALOG-EMPHASIZE = 'C610'.
* MODIFY T_CATALOG FROM FS_CATALOG.
when 'CARRID'.
fs_catalog-hotspot = 'X'.
modify t_catalog from fs_catalog.
when 'CONNID'.
fs_catalog-no_out = 'X'.
modify t_catalog from fs_catalog.
when 'PRICE'.
* fs_catalog-no_sum = 'X'.
fs_catalog-emphasize = 'C310'.
modify t_catalog from fs_catalog.
when 'PLANETYPE'.
fs_catalog-icon = 'X'.
modify t_catalog from fs_catalog.
endcase.

endloop.

clear fs_catalog.

FS_CATALOG-FIELDNAME = 'CHECK'.
FS_CATALOG-CHECKBOX = 'X'.
FS_CATALOG-EMPHASIZE = 'C610'.
FS_CATALOG-OUTPUTLEN = '3'.
FS_CATALOG-COLTEXT = 'BOX'.
APPEND FS_CATALOG TO T_CATALOG.
read table t_sflight index 5 into fs_sflight.
fs_color-fname = 'SEATSMAX'.
fs_color-color-col = '7'.
fs_color-color-int = '1'.
append fs_color to fs_sflight-t_color.
modify t_sflight index 5 from fs_sflight.
read table t_sflight index 7 into fs_sflight.
fs_color-fname = ' '.
fs_color-color-col = '5'.
fs_color-color-int = '1'.
append fs_color to fs_sflight-t_color.
modify t_sflight index 7 from fs_sflight.


call method w_grid->set_table_for_first_display
exporting
is_layout = fs_layout
* i_structure_name = 'SBOOK'
changing
it_outtab = t_sflight
it_fieldcatalog = t_catalog.

endmodule. "STATUS_1985 OUTPUT

*---------------------------------------------------------------------*
* MODULE USER_COMMAND_1985 INPUT
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
module user_command_1985 input.
leave program.

<b>Reward if usefull</b>

Read only

Former Member
0 Likes
850

Hi

Coloring a row is a bit more complicated. To enable row coloring, you should add an additional field to your list data table. It should be of character type and length at least 4. This field will contain the color code for the row. So, let’s modify declaration of our list data table “gt_list”.

Adding the field that will contain row color data

As you guess, you should fill the color code to this field. Its format will be the same as explained before at section C.6.3. But how will ALV Grid know that you have loaded the color data for the row to this field. So, you make it know this by

*--- Internal table holding list data

DATA BEGIN OF gt_list OCCURS 0 .

INCLUDE STRUCTURE SFLIGHT .

DATA rowcolor(4) TYPE c .

DATA END OF gt_list . passing the name of the field containing color codes to the field “INFO_FNAME” of the layout structure.

e.g.

ps_layout-info_fname = <field_name_containing_color_codes>. “e.g. ‘ROWCOLOR’

You can fill that field anytime during execution. But, of course, due to the flow logic of screens, it will be reflected to your list display as soon as an ALV refresh occurs.

You can color an entire row as described in the next section. However, this method is less time consuming.

Coloring Individual Cells

This is the last point about coloring procedures for the ALV Grid. The procedure is similar to coloring an entire row. However, since an individual cell can be addressed with two parameters we will need something more. What is meant by “more” is a table type structure to be included into the structure of the list data table. It seems strange, because including it will make our list data structure deep. But anyhow ALV Grid control handles this.

The structure that should be included must be of type “LVC_T_SCOL”. If you want to color the entire row, this inner table should contain only one row with field “fname” is set to space, some color value at field “col”, “0” or “1” at fields “int” (intensified) and “inv” (inverse).

If you want to color individual cells, then for each cell column, append a line to this inner table which also contains the column name at field “fname”. It is obvious that you can color an entire column by filling this inner table with a row for that column for each row in the list data table. But, it is also obvious that, this will be more time consuming than the method at section C.6.1.

Again key field coloring will override your settings. That’s why, we have another field in this inner table called “nokeycol”. For each field represented in the inner table, set this field to ‘X’ to prevent overriding of key color settings.

In this procedure, again we must tell the control the name of the inner table containing color data. The field “CTAB_FNAME” of the layout structure is used for this purpose.

Adding inner table that will contain cell color data

A sample code to make the cell at row 5 and column ‘SEATSOCC’ colored

*--- Internal table holding list data

DATA BEGIN OF gt_list OCCURS 0 .

INCLUDE STRUCTURE SFLIGHT .

DATA rowcolor(4) TYPE c .

DATA cellcolors TYPE lvc_t_scol .

DATA END OF gt_list .

DATA ls_cellcolor TYPE lvc_s_scol .

...

READ TABLE gt_list INDEX 5 .

ls_cellcolor-fname = 'SEATSOCC' .

ls_cellcolor-color-col = '7' .

ls_cellcolor-color-int = '1' .

APPEND ls_cellcolor TO gt_list-cellcolors .

MODIFY gt_list INDEX 5 .

A juicy-brained guy may ask what happens if all these three procedures applied for coloring at the same time. And again the same answer is given as there is a priority among them. The priority order is: cell setting - row setting - column setting. Beside these, key field setting must be handled.

<b>Reward if usefull</b>