Tuesday, July 23, 2013
For simple ALV list output, use REUSE_ALV_LIST_DISPLAY function module.


REPORT ZALV_LIST_PROGRAM.
*&---------------------------------------------------------------------*
*& Declaration part
*&---------------------------------------------------------------------*
"Types
TYPES:
    BEGIN OF T__DATA,
      MATNR  TYPE MARD-MATNR,
      WERKS  TYPE MARD-WERKS,
      LGORT  TYPE MARD-LGORT,
    END OF T__DATA.
"Work Areas
DATA:W__DATA TYPE T__DATA.
"Internal tables
DATA:I__DATA TYPE STANDARD TABLE OF T__DATA.
*&---------------------------------------------------------------------*
* 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__DATA
  UP TO 10 ROWS.
  IF I__DATA[] IS INITIAL.
    "Dummy data
    DO 10 TIMES.
      W__DATA-MATNR = SY-INDEX.
      W__DATA-WERKS = SY-INDEX + 1.
      W__DATA-LGORT = SY-INDEX + 2.
      APPEND W__DATA TO I__DATA.
      CLEAR  W__DATA.
    ENDDO.
  ENDIF.

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

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