Saturday, December 18, 2010

Define one variable(LIGHTS) for traffic lights in ITAB that is to be displayed on ALV. Pass the variable name and ITAB name in the layout structure.
Steps
  • Define LIGHTS variable in the internal table
  • Pass LIGHTS variable through WA_LAYOUT-LIGHTS_FIELDNAME.
  • Pass 1(= red) or 2(=yellow) or 3(=green) to lights fields.

REPORT ZTEST_ALV_TRAFFIC_LIGHTS .
TYPE-POOLS:icon.
DATA:
      BEGIN OF IT_LIGHTS OCCURS 0,
       LIGHTS TYPE CHAR4,"Var is needs to be declared with length 4 char
MATNR TYPE MARD-MATNR, WERKS TYPE MARD-WERKS, LGORT TYPE MARD-LGORT,
      END OF IT_LIGHTS.
TYPE-POOLS:SLIS.
TYPES:
      T_FIELDCAT         TYPE SLIS_FIELDCAT_ALV,
      T_EVENTS           TYPE SLIS_ALV_EVENT,
      T_LAYOUT           TYPE SLIS_LAYOUT_ALV.
DATA:
      W_FIELDCAT         TYPE T_FIELDCAT,
      W_EVENTS           TYPE T_EVENTS,
      W_LAYOUT           TYPE T_LAYOUT.
DATA:
      I_FIELDCAT         TYPE STANDARD TABLE OF T_FIELDCAT,
      I_EVENTS           TYPE STANDARD TABLE OF T_EVENTS.

START-OF-SELECTION.

  SELECT MATNR WERKS LGORT FROM MARD INTO CORRESPONDING FIELDS OF TABLE IT_LIGHTS UP TO 10 ROWS.
  IF IT_LIGHTS[] IS INITIAL.
    DO 10 TIMES.
      IT_LIGHTS-MATNR = SY-INDEX.
      IT_LIGHTS-WERKS = SY-INDEX + 1.
      IT_LIGHTS-LGORT = SY-INDEX + 2.
      APPEND IT_LIGHTS TO IT_LIGHTS.
      CLEAR  IT_LIGHTS.
    ENDDO.
  ENDIF.
  "Just pass 1=red or 2=yellow or 3=green to lights fields
  LOOP AT IT_LIGHTS INTO IT_LIGHTS.
    IF SY-TABIX BETWEEN 1 AND 3.
      IT_LIGHTS-LIGHTS = '1'.
    ELSEIF SY-TABIX BETWEEN 4 AND 7.
      IT_LIGHTS-LIGHTS = '2'.
    ELSEIF SY-TABIX BETWEEN 8 AND 10.
      IT_LIGHTS-LIGHTS = '3'.
    ENDIF.
    MODIFY IT_LIGHTS FROM IT_LIGHTS INDEX SY-TABIX TRANSPORTING LIGHTS.
  ENDLOOP.
  
  DEFINE FIELDCAT.
    W_FIELDCAT-FIELDNAME = &1.
    W_FIELDCAT-TABNAME   = &2.
    W_FIELDCAT-SELTEXT_M = &3.
    APPEND W_FIELDCAT TO I_FIELDCAT.
    CLEAR  W_FIELDCAT.
  END-OF-DEFINITION.
  FIELDCAT: 
        'MATNR'  'IT_LIGHTS' 'MARD-MATNR',
        'WERKS'  'IT_LIGHTS' 'MARD-WERKS',
        'LGORT'  'IT_LIGHTS' 'MARD-LGORT'.
  
  W_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  W_LAYOUT-ZEBRA             = 'X'.
  W_LAYOUT-LIGHTS_FIELDNAME  = 'LIGHTS'.
  W_LAYOUT-LIGHTS_TABNAME    = 'I_LIGHTS'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IS_LAYOUT          = W_LAYOUT
      IT_FIELDCAT        = I_FIELDCAT
    TABLES
      T_OUTTAB           = IT_LIGHTS.

Output

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.