Sunday, December 19, 2010
Define LIGHTS field(type C length 4) to pass icon value to the record of the ALV output.

REPORT zvenkat_alv_traffic_lights .
*&--------------------------------------------------------------------*
*& Declaration part
*&---------------------------------------------------------------------*
"Types
TYPES:
    BEGIN OF t_lights,
      matnr  TYPE mard-matnr,
      werks  TYPE mard-werks,
      lgort  TYPE mard-lgort,
      lights TYPE char4,       "Variable is needs to be declared with length 4 char
    END OF t_lights.
"Work Areas
DATA:
    w_lights TYPE t_lights.
"Internal tables
DATA:
    i_lights TYPE STANDARD TABLE OF t_lights.
*&---------------------------------------------------------------------*
* ALV Declarations
*----------------------------------------------------------------------*
* Types Pools
TYPE-POOLS:
    slis.
* Types
TYPES:
    t_fieldcat         TYPE slis_fieldcat_alv,
    t_events           TYPE slis_alv_event,
    t_layout           TYPE slis_layout_alv.
* Workareas
DATA:
    w_fieldcat         TYPE t_fieldcat,
    w_events           TYPE t_events,
    w_layout           TYPE t_layout.
* Internal Tables
DATA:
    i_fieldcat         TYPE STANDARD TABLE OF t_fieldcat,
    i_events           TYPE STANDARD TABLE OF t_events.
*&---------------------------------------------------------------------*
*&    start of selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_data.

*&---------------------------------------------------------------------*
*&    end-of-selection.
*&---------------------------------------------------------------------*
END-OF-SELECTION.

  PERFORM build_fieldcatlog.
  PERFORM build_layout.
  PERFORM list_display.
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
FORM get_data .

  SELECT matnr
  werks
  lgort
  FROM mard
  INTO CORRESPONDING FIELDS OF TABLE i_lights
  UP TO 10 ROWS.
  IF i_lights[] IS INITIAL.
    "Dummy data
    DO 10 TIMES.
      w_lights-matnr = sy-index.
      w_lights-werks = sy-index + 1.
      w_lights-lgort = sy-index + 2.
      APPEND w_lights TO i_lights.
      CLEAR  w_lights.
    ENDDO.
  ENDIF.
  "Just pass 1=red or 2=yellow or 3=green to lights fields
  LOOP AT i_lights INTO w_lights .
    IF sy-tabix BETWEEN 1 AND 3.
      w_lights-lights = '1'.
    ELSEIF sy-tabix BETWEEN 4 AND 7.
      w_lights-lights = '2'.
    ELSEIF sy-tabix BETWEEN 8 AND 10.
      w_lights-lights = '3'.
    ENDIF.
    MODIFY i_lights FROM w_lights INDEX sy-tabix TRANSPORTING lights.
  ENDLOOP.

ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  build_fieldcatlog
*&---------------------------------------------------------------------*
FORM build_fieldcatlog .
  CLEAR:w_fieldcat,i_fieldcat[].

  w_fieldcat-fieldname     = 'MATNR'.
  w_fieldcat-seltext_m     = 'MATNR'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname     = 'WERKS'.
  w_fieldcat-seltext_m     = 'WERKS'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname     = 'LGORT'.
  w_fieldcat-seltext_m     = 'LGORT'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.

ENDFORM.                    " build_fieldcatlog
*&---------------------------------------------------------------------*
*&      Form  build_layout
*&---------------------------------------------------------------------*
FORM build_layout .

  w_layout-colwidth_optimize = 'X'.
  w_layout-zebra             = 'X'.
  w_layout-lights_fieldname  = 'LIGHTS'.
  w_layout-lights_tabname    = 'I_LIGHTS'.
  w_layout-lights_rollname   = 'Hits'.

ENDFORM.                    " build_layout
*&---------------------------------------------------------------------*
*&      Form  list_display
*&---------------------------------------------------------------------*
FORM list_display .
  DATA:
      l_program TYPE sy-repid.
  l_program = sy-repid.

  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      i_callback_program = l_program
      is_layout          = w_layout
      it_fieldcat        = i_fieldcat
    TABLES
      t_outtab           = i_lights
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  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.                    " list_display

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.