Раскраска ячеек в ALV GRID SAP ABAP Coloring a cell

This document describes how to color a cell make the particular cell as non-editable display the cell as button in ALV using OOPS method.

Coloring a cell

Step 1: Include a field called cellcolor in output table as below. Create a work area for the cellcolor.
Code:

TYPES : BEGIN OF ty.
INCLUDE STRUCTURE mara.
* For cell coloring
TYPES : cellcolor TYPE lvc_t_scol,
END OF ty.
Data : w_cellcolor TYPE lvc_s_scol, "For cell color

Step 2: In layout, mention the field name for ctab_fname
Code:

* Setting layout
w_layout-ctab_fname = 'CELLCOLOR'."For cell coloring

Step 3: Mention the field name for coloring and then set the color, intensified and inverse options.
Code:

* Colouring a cell
clear w_cellcolor.
w_cellcolor-fname = 'ERSDA'.
w_cellcolor-color-col = '5'.
w_cellcolor-color-int = '1'.
w_cellcolor-color-inv = '1'.
APPEND w_cellcolor TO wa-cellcolor.
MODIFY itab FROM wa INDEX 7 TRANSPORTING cellcolor.

Here you can set the col to different numbers(1 to 9) and then give intensified/ inverse values either 0 or 1 to know the variations.

Step 4: Pass the required parameters for set_table_for_first_display as below.
Code:

* Displaying the output
CALL METHOD o_grid->set_table_for_first_display
EXPORTING
is_variant                    = w_variant
i_save                        = 'A'
is_layout                     = w_layout
CHANGING
it_outtab                     = itab
it_fieldcatalog               = i_fieldcat
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.

Displaying the cell as button
Step 1: Include a field called cellstyles in output table as below. Create a work area for the styles.
Code:

TYPES : BEGIN OF ty.
INCLUDE STRUCTURE mara.
* For cell editing and displaying cell as push button
TYPES : cellstyles TYPE lvc_t_styl,
END OF ty.
Data w_style TYPE lvc_s_styl.

Step 2: Set the layout stylefname as CELLSTYLES.
Code:

* Setting layout
w_layout-stylefname = 'CELLSTYLES' ."cell-push button and edit

Step 3: Assign mc_style_button to style to display the field as button. You can find those details in cl_gui_alv_grid class’s attribute.

Code:

* Displaying cell as Push button
CLEAR w_style.
w_style-fieldname = 'ERNAM' .
w_style-style = cl_gui_alv_grid->mc_style_button .
APPEND w_style TO wa-cellstyles.
MODIFY itab FROM wa INDEX 1 TRANSPORTING cellstyles.

Step 4: Pass the layout for set_table_for_first_display.
Code:

* Displaying the output
CALL METHOD o_grid->set_table_for_first_display
EXPORTING
is_variant                    = w_variant
i_save                        = 'A'
is_layout                     = w_layout
CHANGING
it_outtab                     = itab
it_fieldcatalog               = i_fieldcat
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.

Making the cell as Editable and non-editable
Follow Step 1 and Step 2 for displaying cell as button.

Step 3: Make the entire column as editable and then disable the edit option for a cell for that column. Here in this example, for ERNAM we are making the third row as non-editable.

Code:

* Making an entire column as Editable.
FIELD-SYMBOLS : <fs_fieldcat>; TYPE lvc_s_fcat.
LOOP AT i_fieldcat ASSIGNING <fs_fieldcat>;.
CASE <fs_fieldcat>-fieldname.
WHEN 'ERNAM'.
* Making a column as Editable
<fs_fieldcat>-edit = 'X'.
ENDCASE.
ENDLOOP.
* Making a particular cell as non-editable and other editable
CLEAR w_style.
w_style-fieldname = 'ERNAM'.
w_style-style = cl_gui_alv_grid->mc_style_disabled.
REFRESH wa-cellstyles.
APPEND w_style TO wa-cellstyles.
MODIFY itab FROM wa INDEX 3 TRANSPORTING cellstyles.

Follow Step4 for displaying cell as button.

Complete Code
Screen 9000,GUI Status ZSTATUS and GUI Title ZTITLE should be created and in Flow logic of the screen, PBO and PAI should be uncommented.

Code:

* Data Declaration
TYPES : BEGIN OF ty.
INCLUDE STRUCTURE mara.
* For cell editing and displaying cell as push button
TYPES : cellstyles TYPE lvc_t_styl ,
* For cell coloring
cellcolor TYPE lvc_t_scol,
END OF ty.

DATA : itab TYPE STANDARD TABLE OF ty,"Output Internal table
i_fieldcat TYPE STANDARD TABLE OF lvc_s_fcat,"Field catalog
wa TYPE ty,
w_variant TYPE disvariant,
w_layout TYPE lvc_s_layo,"Layout structure
w_cellcolor TYPE lvc_s_scol, "For cell color
w_style TYPE lvc_s_styl, "cell editing and
"displaying cell as push button
o_docking TYPE REF TO cl_gui_docking_container,"Docking Container
o_grid TYPE REF TO cl_gui_alv_grid."Grid

FIELD-SYMBOLS : <fs_fieldcat> TYPE lvc_s_fcat.

SELECT * FROM mara INTO CORRESPONDING FIELDS OF TABLE itab UP TO 10 ROWS
.

CALL SCREEN 9000.
*&amp;---------------------------------------------------------------------*
*&amp; Module STATUS_9000 OUTPUT
*&amp;---------------------------------------------------------------------*
* PBO
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.

IF o_docking IS INITIAL.
SET PF-STATUS 'ZSTATUS'. "GUI Status
SET TITLEBAR 'ZTITLE'. "Title
* Creating Docking Container and grid
PERFORM create_object.
* Filling the fieldcatalog table
PERFORM create_fieldcat.
* Setting layout
PERFORM set_layout.
* Colouring a cell
PERFORM color_cell.
* Displaying cell as Push button
PERFORM cell_button.
* Making a cell as non-editable in a column
PERFORM cell_edit.
* Displaying the output
PERFORM display_output.
ENDIF.

ENDMODULE. " STATUS_9000 OUTPUT

*&amp;---------------------------------------------------------------------*
*&amp; Module USER_COMMAND_9000 INPUT
*&amp;---------------------------------------------------------------------*
* PAI
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

DATA lv_ucomm TYPE sy-ucomm.
lv_ucomm = sy-ucomm.
CASE lv_ucomm.
WHEN 'CANCEl' OR 'EXIT'.
PERFORM free_objects.
LEAVE PROGRAM.
WHEN 'BACK'.
PERFORM free_objects.
SET SCREEN '0'.
LEAVE SCREEN.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT

*&amp;---------------------------------------------------------------------*
*&amp; Form free_objects
*&amp;---------------------------------------------------------------------*
* Free Objects
*----------------------------------------------------------------------*
FORM free_objects .
CALL METHOD o_grid->free
EXCEPTIONS
cntl_error = 1
cntl_system_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.

CALL METHOD o_docking->free
EXCEPTIONS
cntl_error = 1
cntl_system_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.
ENDFORM. " free_objects

*&amp;---------------------------------------------------------------------*
*&amp; Form create_object
*&amp;---------------------------------------------------------------------*
* Creating Docking Container and grid
*----------------------------------------------------------------------*
FORM create_object .
* Creating Docking Container
CREATE OBJECT o_docking
EXPORTING
ratio = '95'.
IF sy-subrc EQ 0.
* Creating Grid
CREATE OBJECT o_grid
EXPORTING
i_parent = o_docking.
ENDIF.
ENDFORM. " create_object

*&amp;---------------------------------------------------------------------*
*&amp; Form create_fieldcat
*&amp;---------------------------------------------------------------------*
* Filling the fieldcatalog table

Добавить комментарий