Skip to end of metadata
Go to start of metadata

Author: J.Jayanthi
Submitted: 06-Jul-2007
Related Links:

         ABAP-7 Steps to create OOPS ALV(for beginners)

        Coloring a Row and Column in ALV (OOPS)

Description:

This document describes how to

  1. color a cell
  2. make the particular cell as non-editable
  3. display the cell as button

in ALV using OOPS method.

 Procedure

Coloring a cell

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

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
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

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
 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.
* Setting layout

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
   w_layout-stylefname = 'CELLSTYLES' ."cell-push button and edit

t
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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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.

* Making an entire column as Editable.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
FIELD-SYMBOLS :  TYPE lvc_s_fcat.
  LOOP AT i_fieldcat ASSIGNING .
    CASE -fieldname.
      WHEN 'ERNAM'.
* Making a column as Editable
        -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.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
* 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> TYPE lvc_s_fcat.

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

*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* 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
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* 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
*&---------------------------------------------------------------------*
*& Form free_objects
*&---------------------------------------------------------------------*
* 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
*&---------------------------------------------------------------------*
*& Form create_object
*&---------------------------------------------------------------------*
* 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
*&---------------------------------------------------------------------*
*& Form create_fieldcat
*&---------------------------------------------------------------------*
* Filling the fieldcatalog table
*----------------------------------------------------------------------*
FORM create_fieldcat .
* Filling the fieldcatalog table
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'MARA'
    CHANGING
      ct_fieldcat            = i_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
ENDFORM. " create_fieldcat
*&---------------------------------------------------------------------*
*& Form set_layout
*&---------------------------------------------------------------------*
* Setting layout
*----------------------------------------------------------------------*
FORM set_layout .
  w_variant-report = sy-repid.
* Setting layout
  w_layout-stylefname = 'CELLSTYLES' ."cell-push button and edit
  w_layout-ctab_fname = 'CELLCOLOR'."For cell coloring
ENDFORM. " set_layout
*&---------------------------------------------------------------------*
*& Form color_cell
*&---------------------------------------------------------------------*
* Colouring a cell
*----------------------------------------------------------------------*
FORM color_cell .
* 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.
ENDFORM. " color_cell
*&---------------------------------------------------------------------*
*& Form cell_button
*&---------------------------------------------------------------------*
* Displaying cell as Push button
*----------------------------------------------------------------------*
FORM cell_button .
* 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.
ENDFORM. " cell_button
*&---------------------------------------------------------------------*
*& Form cell_edit
*&---------------------------------------------------------------------*
* Making a cell as non-editable in a column
*----------------------------------------------------------------------*
FORM cell_edit .
  LOOP AT i_fieldcat ASSIGNING <fs> .
    CASE <fs>-fieldname.
      WHEN 'ERNAM'.
* Making a column as Editable
        <fs>-edit = 'X'.
    ENDCASE.
  ENDLOOP.
* Making a particular cell as non-editable and other editable. Similarly we can make a
* particular cell as non-editable based on some condition.
  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.
ENDFORM. " cell_edit
*&---------------------------------------------------------------------*
*& Form display_output
*&---------------------------------------------------------------------*
* Displaying the output
*----------------------------------------------------------------------*
FORM display_output .
* 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.
ENDFORM. " display_output

Output

Field : Created on (7th row is colored)

Field : Created by (1st row is shown as button)

Field : Created by (3rd row is non-editable while others are editable(except button))