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

ALV_GRID Functionality

Former Member
0 Likes
696

Hi All,

I am working on ALV report and I want to add this functionality:

I have got two dates : it_indate and it_endate

and if the time period between these two dates is more than 20 then I want to highlight or color the following fields in my report in the final output:

1. BELNR

2. EBELN

3. SAKNR

and when user scroll right or left to view all the fields in the output I want these fields to be intact, I mean I don't want these fields to move.

Can you please tell me how can i add this thing in my report. I posted thing before also but didn't get any response, please try to help.

Thanks,

1 ACCEPTED SOLUTION
Read only

Former Member
4 REPLIES 4
Read only

Former Member
Read only

Former Member
0 Likes
619

to not get them to move set the -KEY parm in the field catalog to 'X'.

For coloring an individual cell, you'll need to be doing the ALV with OO and not ALV_GRID_REUSE.

Look into the use of CELLSTYLES at this link http://www.abap4.it/download/ALV.pdf

REUSE will allow Column color, I haven't found a way to do an individual Cell

Here is some code I played with to demo cell coloring in OO

Screen 2000 had nothing on it but the Custom Control and the OK_CODE field


REPORT z_alv_template.

TABLES: kna1.

* Data (for the ALV Grid)
TYPES:
  BEGIN OF t_alv_data,
    cust_id    TYPE kunnr,        "Customer Number
    cust_name  TYPE name1_gp,     "Customer Name
    cust_color TYPE i,
*   cell coloring field
    color     TYPE lvc_t_scol,   "Cell coloring
  END OF t_alv_data.

DATA:
  v_alv_data TYPE t_alv_data,
  i_alv_data TYPE STANDARD TABLE OF t_alv_data.


* ALV grid containers and objects
DATA:
  o_alv_grid TYPE REF TO cl_gui_alv_grid,
  o_alv_cont TYPE REF TO cl_gui_custom_container.


* ALV field catalog
DATA:
  i_alv_fc TYPE lvc_t_fcat,
  v_alv_fc LIKE lvc_s_fcat.


* ALV Layout (colors)
DATA:
  v_alv_layout TYPE lvc_s_layo,
  i_alv_color TYPE lvc_t_scol,
  v_alv_color TYPE lvc_s_scol,
  v_alv_color_cell TYPE lvc_s_colo.


* ALV variant
DATA:
  v_alv_variant  TYPE disvariant.
PARAMETERS:
  p_alvvar TYPE disvariant-variant DEFAULT 'DEFAULT'.

DATA: ok_code LIKE sy-ucomm.

* Class for event handling

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
* [+] Event listener for the ALV grid
* [+] Handles hotspots and data changes
*----------------------------------------------------------------------*

CLASS lcl_event_receiver DEFINITION.

  PUBLIC SECTION.

    METHODS:

*     Hotspot clicking
      hotspot_click
           FOR EVENT hotspot_click OF cl_gui_alv_grid
             IMPORTING e_row_id
                       e_column_id
                       es_row_no,

*     Data changed (such as checkbox clicking)
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
            IMPORTING er_data_changed.

ENDCLASS.                    "lcl_event_receiver DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
* [+] Implementation of the ALV Grid event handler class
*---------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.


*---------------------------------------------------------------------*
*       METHOD hotspot_click                                          *
*---------------------------------------------------------------------*
* [+] Calls evvent_hotspot_click when a hotspot is clicked in the ALV
*---------------------------------------------------------------------*
  METHOD hotspot_click.
    PERFORM event_hotspot_click
                  USING e_row_id
                        e_column_id.
  ENDMETHOD.                    "hotspot_click


*---------------------------------------------------------------------*
*       METHOD handle_data_changed                                    *
*---------------------------------------------------------------------*
* [+] Updates the source data when the data in the ALV display has
* been changed, such as by clicking a checkbox.
*---------------------------------------------------------------------*
  METHOD handle_data_changed.

    DATA: lv_changed TYPE lvc_s_modi.

    LOOP AT er_data_changed->mt_good_cells INTO lv_changed
      WHERE fieldname = 'CUST_NAME'.

      READ TABLE i_alv_data INTO v_alv_data INDEX lv_changed-row_id.

      IF sy-subrc = 0.
        MOVE lv_changed-value TO v_alv_data-cust_name.
        MODIFY i_alv_data FROM v_alv_data INDEX lv_changed-row_id.
      ENDIF.

    ENDLOOP.

  ENDMETHOD.                    "handle_data_changed

ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION


* Reference to the event listener class
DATA: event_receiver TYPE REF TO lcl_event_receiver.

*---------------------------------------------------------------------*
*       FORM build_event_listener
*---------------------------------------------------------------------*
* [+] Set the event handler on the ALV Grid
*---------------------------------------------------------------------*
FORM build_event_listener.

* Assigning the event listener to the ALV
  CREATE OBJECT event_receiver.

  SET HANDLER event_receiver->handle_data_changed FOR o_alv_grid.
  SET HANDLER event_receiver->hotspot_click       FOR o_alv_grid.

ENDFORM.                    "build_event_listener


*---------------------------------------------------------------------*
*       AT SELECTION-SCREEN
*         ON VALUE-REQUEST FOR p_alvvar
*---------------------------------------------------------------------*
* [+] Calls choose_alv_variant to ask the user to select an alv grid
*     layout variant
*---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_alvvar.
  PERFORM choose_alv_variant
    CHANGING
      p_alvvar
      v_alv_variant.


*---------------------------------------------------------------------*
*       START_OF_SELECTION
*---------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM get_data.

  SET PF-STATUS 'ALVSCREEN' IMMEDIATELY.

  CALL SCREEN 2000.


*---------------------------------------------------------------------*
*       FORM get_data
*---------------------------------------------------------------------*
* [+] Gets the data for the ALV grid
*---------------------------------------------------------------------*
FORM get_data.

  SELECT kunnr name1
    INTO (v_alv_data-cust_id,
          v_alv_data-cust_name)
    FROM kna1.

    APPEND v_alv_data TO i_alv_data.

  ENDSELECT.

ENDFORM.                    "get_data


*---------------------------------------------------------------------*
*       MODULE build_alv_grid
*---------------------------------------------------------------------*
*
*   THIS SHOULD BE IN THE "PROCESS BEFORE OUTPUT" OF THE ALV SCREEN
*
* [+] Builds the ALV Grid objects
* [+] Calls to build the field catalog table
* [+] Loads the field catalog table into the ALV Grid
* [+] Loads the table data into the ALV Grid
*---------------------------------------------------------------------*
MODULE build_alv_grid OUTPUT.

  SET TITLEBAR  '2000'.

* Also enables layout saving
  PERFORM set_alv_variant
    USING
      p_alvvar
    CHANGING
      v_alv_variant.


* Building the grid and container on the screen
* NOTE: the container name MUST be upper-case
* Also, we don't want the objects to be created if in batch mode!
  IF sy-batch IS INITIAL.
    CREATE OBJECT o_alv_cont
      EXPORTING
        container_name = 'O_ALV_TABLE'.
  ENDIF.

  CREATE OBJECT o_alv_grid
    EXPORTING
      i_parent = o_alv_cont.


* builds the event listener
  PERFORM build_event_listener.


* Color the cells
  PERFORM color_cells.


* Build the field catalog
  PERFORM build_alv_fc.


* Loads the data into the grid
  CALL METHOD o_alv_grid->set_table_for_first_display
    EXPORTING
      i_save          = 'A'
      i_default       = 'X'
      is_variant      = v_alv_variant
      is_layout       = v_alv_layout
    CHANGING
      it_outtab       = i_alv_data
      it_fieldcatalog = i_alv_fc.


ENDMODULE.                    "build_alv_grid OUTPUT


*---------------------------------------------------------------------*
*       FORM build_alv_fc
*---------------------------------------------------------------------*
* [+] Constructs the ALV Grid field catalog table
*---------------------------------------------------------------------*
FORM build_alv_fc.

  CLEAR i_alv_fc.
  REFRESH i_alv_fc.

* NOTE: the field name MUST be upper-case

*                   field       heading         hide  hot
*                   name                        zero  spot  just
  PERFORM:
    alv_field USING 'CUST_ID'    'Cust ID'        ' '  'X'  'R',
    alv_field USING 'CUST_NAME'  'Customer Name'  ' '  ' '  'L',
    alv_field USING 'CUST_COLOR' 'Color'          ' '  ' '  'R'.

ENDFORM.                    "build_alv_fc


*---------------------------------------------------------------------*
*       FORM alv_field
*---------------------------------------------------------------------*
* [+] Describes and constructs a single field for the ALV Grid field
*     catalog. The field length and type are both obtained from the
*     actual field passed in to this method.
* [+] Adds the constructed field to the ALV Grid field catalog table
*---------------------------------------------------------------------*
FORM alv_field
  USING
    p_field_name TYPE c
    p_heading    TYPE c
    p_hide_zeros TYPE c
    p_hotspot    TYPE c
    p_justify    TYPE c.

  CLEAR v_alv_fc.

  DATA:
    lv_type(1) TYPE c,
    lv_length TYPE i,
    lv_heading_length TYPE i.

* get the type and length of this field
  FIELD-SYMBOLS <field>.
  ASSIGN p_field_name TO <field>.

  DESCRIBE FIELD <field> TYPE lv_type OUTPUT-LENGTH lv_length.

* re-adjust the length to the length of the header, if too short
  lv_heading_length = strlen( p_heading ).

  IF lv_length < lv_heading_length.
    lv_length = lv_heading_length.
  ENDIF.

* NOTE: the field name MUST be upper-case
  v_alv_fc-fieldname = p_field_name.
  TRANSLATE v_alv_fc-fieldname TO UPPER CASE.

  v_alv_fc-inttype   = lv_type.
  v_alv_fc-outputlen = lv_length.
  v_alv_fc-coltext   = p_heading.
  v_alv_fc-seltext   = p_heading.
  v_alv_fc-hotspot   = p_hotspot.

* Determining which fields should show zeros
  IF p_hide_zeros = 'X'.
    v_alv_fc-no_zero = 'X'.
    v_alv_fc-lzero   = ' '.
  ELSE.
    v_alv_fc-no_zero = ' '.
    v_alv_fc-lzero   = 'X'.
  ENDIF.

  v_alv_fc-just = p_justify.

* Add the field to the field catalog
  APPEND v_alv_fc TO i_alv_fc.

ENDFORM.                    "alv_field


*---------------------------------------------------------------------*
*       FORM choose_alv_variant
*---------------------------------------------------------------------*
* [+] Shows a popup that allows the user to choose the layout variant
*     for the alv grid of the current program
* [+] Usually called by an AT SELECTION-SCREEN method.
*---------------------------------------------------------------------*
FORM choose_alv_variant
  CHANGING
    p_variant_name TYPE disvariant-variant
    p_variant      TYPE disvariant.

  CLEAR p_variant.

  DATA:
    p_exit_check(1) TYPE c.

  MOVE sy-repid TO p_variant-report.

  CALL FUNCTION 'LVC_VARIANT_F4'
       EXPORTING
            is_variant = p_variant
            i_save     = 'A'
       IMPORTING
            e_exit     = p_exit_check
            es_variant = p_variant
       EXCEPTIONS
            not_found  = 1
            OTHERS     = 99.

  IF sy-subrc = 0.
    IF p_exit_check <> 'X'.
      p_variant_name = p_variant-variant.
    ENDIF.
  ENDIF.

ENDFORM.                    "choose_alv_variant


*---------------------------------------------------------------------*
*       FORM set_alv_variant
*---------------------------------------------------------------------*
* [+] Sets the alv grid layout variant. Used for setting the variant
*     when its name is entered in a parameter rather than by using the
*     popup, or when loading the variant from a variable of type C
*---------------------------------------------------------------------*
FORM set_alv_variant
  USING
    p_variant_name TYPE disvariant-variant
  CHANGING
    p_variant      TYPE disvariant.

  MOVE sy-repid TO p_variant-report.
  p_variant-variant = p_variant_name.

ENDFORM.                    "set_alv_variant


*---------------------------------------------------------------------*
*       FORM color_cells
*---------------------------------------------------------------------*
* [+] Loop through the data and apply coloring
*---------------------------------------------------------------------*
FORM color_cells.
  DATA:
    my_color  TYPE i.
* tell the ALV grid what field in v_alv_data contains color information
  v_alv_layout-ctab_fname = 'COLOR'.

  my_color = 0.

* loop through each row of the table
  LOOP AT i_alv_data INTO v_alv_data.

*   clear the variables
    CLEAR:
      v_alv_color,
      v_alv_color_cell,
      i_alv_color.

    REFRESH:
      i_alv_color.

    v_alv_data-cust_color = my_color.

    PERFORM color_cell USING 'CUST_COLOR' my_color. "negative

*   apply the colors
*    IF v_alv_data-cust_name = 'Testing Credit'.
*      PERFORM color_cell USING 'CUST_NAME' 6. "negative
*    ELSEIF v_alv_data-cust_name = 'Goober Goober Also'.
*      PERFORM color_cell USING 'CUST_NAME' 5. "positive
*    ENDIF.

*   set the color data for this table row
    v_alv_data-color = i_alv_color.
    MODIFY i_alv_data FROM v_alv_data.

    my_color = my_color + 1.
    IF my_color GT 7.
      CLEAR my_color.
    ENDIF.
  ENDLOOP.

ENDFORM.                    "color_cells


*---------------------------------------------------------------------*
*       FORM color_cell
*---------------------------------------------------------------------*
* [+] Colors a cell in the ALV grid
*---------------------------------------------------------------------*
FORM color_cell
  USING
    p_cellname TYPE c
    p_color    TYPE i.

  CLEAR:
    v_alv_color_cell,
    v_alv_color.

* set the color for the cell
*  IF p_color = 0.
*    v_alv_color_cell-col = 0. "cl_gui_resources=>list_col_background.
*  ELSEIF p_color = 1.
*    v_alv_color_cell-col = 1. "cl_gui_resources=>list_col_heading.
*  ELSEIF p_color = 2.
*    v_alv_color_cell-col = 2. "cl_gui_resources=>list_col_normal.
*  ELSEIF p_color = 3.
*    v_alv_color_cell-col = 3. "cl_gui_resources=>list_col_total.
*  ELSEIF p_color = 4.
*    v_alv_color_cell-col = 4. "cl_gui_resources=>list_col_key.
*  ELSEIF p_color = 5.
*    v_alv_color_cell-col = 5. "cl_gui_resources=>list_col_positive.
*  ELSEIF p_color = 6.
*    v_alv_color_cell-col = 6. "cl_gui_resources=>list_col_negative.
*  ELSEIF p_color = 7.
*    v_alv_color_cell-col = 7. "cl_gui_resources=>list_col_group.
*  ENDIF.

  v_alv_color_cell-col = p_color.

  v_alv_color-nokeycol = 'X'.
  v_alv_color-fname = p_cellname.
*  v_alv_color-color = p_color.
  v_alv_color-color = v_alv_color_cell.
  APPEND v_alv_color TO i_alv_color.

ENDFORM.                    "color_cell



*---------------------------------------------------------------------*
*       FORM event_hotspot_click
*---------------------------------------------------------------------*
* [+] What to do when clicking on a hotspot in the ALV Grid
*---------------------------------------------------------------------*
FORM event_hotspot_click
  USING
    p_row    TYPE lvc_s_row
    p_column TYPE lvc_s_col.

  DATA:
    lv_docnum TYPE kunnr.

  READ TABLE i_alv_data INTO v_alv_data INDEX p_row-index.

  IF p_column = 'CUST_ID'.
*   call a transaction when the cust_id is clicked
    SET PARAMETER ID 'AUN' FIELD v_alv_data-cust_id.
    CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
  ENDIF.

ENDFORM.                    "event_hotspot_click
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_2000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_2000 INPUT.
  CASE ok_code.
    WHEN 'BACK'
      OR 'STOP'
      OR 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_2000  INPUT

Read only

0 Likes
619
Read only

Former Member
0 Likes
619

hi check this ....

if the date field value gt 20 then you can write like this....

data: date1 type sy-datum value '20080422' ,

date2 type sy-datum value '20080401' ,

date3 type sy-datum .

date3 = date1 - date2 .

it_fieldcat-fieldname = 'FIELD'.

it_fieldcat-reptext_ddic = 'description'.

if date3 ge 20 .

it_fieldcat-emphasize = 'C111'.

endif .

regards,

venkat.