Sunday, September 25, 2016

Create an ALV report using CL_GUI_ALV_GRID

The ALV report is created as following steps:
  • Create screen 9001, drop the custom control with name ‘ALV_CONT’
  • Create a custom container (ref to CL_GUI_DOCKING_CONTAINER) with parameter CONTAINER NAME = ‘ALV_CONT’
  • Create ALV grid object inside the container (ref to CL_GUI_ALV_GRID)
  • Get the field catalog
  • Call the method of alv grid set_table_for_first_display to display the output


The code sample is as below:

File 1:
REPORT ZPG_DT_ALV_SAMPLE.
INCLUDE ZPG_DT_ALV_SAMPLE_GET_DATAF01.

INITIALIZATION.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FNAME.
 CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     PROGRAM_NAME  = SYST-CPROG
     DYNPRO_NUMBER = SYST-DYNNR
     FIELD_NAME    = 'P_FNAME'
   IMPORTING
     FILE_NAME     = P_FNAME
 .

START-OF-SELECTION.
 PERFORM GET_DATA.
 PERFORM PROCESS_DATA.
 PERFORM DISPLAY_DATA.

File 2:
*----------------------------------------------------------------------*
***INCLUDE ZPG_DT_ALV_SAMPLETOP.
*----------------------------------------------------------------------*

TYPES:
 BEGIN OF TY_TRANSACTION,
   TICKER(10) TYPE C,
   DATE       TYPE DATE,
   OPEN       TYPE F,
   HIGH       TYPE F,
   LOW        TYPE F,
   CLOSE      TYPE F,
   VOLUME     TYPE I,
 END OF TY_TRANSACTION.

DATA:
 W_FIELDCAT TYPE LVC_S_FCAT,

 O_CONTAINER  TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
 O_GRID       TYPE REF TO CL_GUI_ALV_GRID.

DATA:

 T_INFILE   TYPE TABLE OF TY_TRANSACTION,
 T_FIELDCAT TYPE LVC_T_FCAT.

* Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK B1.
PARAMETERS: P_FNAME TYPE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK B1.

File 3:
*----------------------------------------------------------------------*
***INCLUDE ZPG_DT_ALV_SAMPLE_GET_DATAF01.
*----------------------------------------------------------------------*

INCLUDE ZPG_DT_ALV_SAMPLETOP.


FORM GET_DATA .
 DATA: L_FILENAME TYPE STRING.
 L_FILENAME = P_FNAME.
 CALL FUNCTION 'GUI_UPLOAD'
     EXPORTING
       FILENAME            = L_FILENAME    "File path
       FILETYPE            = 'ASC'         "File type as ASCII
       HAS_FIELD_SEPARATOR = 'X'
     TABLES
       DATA_TAB            = T_INFILE
     EXCEPTIONS
       OTHERS              = 0.


 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.


FORM PROCESS_DATA .

 PERFORM SET_FIELDCAT.

ENDFORM.


FORM DISPLAY_DATA .

* Create docking container
 CREATE OBJECT O_CONTAINER
   EXPORTING
*      PARENT                      =
     CONTAINER_NAME              = 'ALV_CONT'
*      STYLE                       =
*      LIFETIME                    = lifetime_default
*      REPID                       =
*      DYNNR                       =
*      NO_AUTODEF_PROGID_DYNNR     =
*    EXCEPTIONS
*      CNTL_ERROR                  = 1
*      CNTL_SYSTEM_ERROR           = 2
*      CREATE_ERROR                = 3
*      LIFETIME_ERROR              = 4
*      LIFETIME_DYNPRO_DYNPRO_LINK = 5
*      OTHERS                      = 6
     .
 IF SY-SUBRC = 0.

*   Create Grid inside container
   CREATE OBJECT O_GRID
     EXPORTING
*      I_SHELLSTYLE      = 0
*      I_LIFETIME        =
       I_PARENT          = O_CONTAINER
*      I_APPL_EVENTS     = space
*      I_PARENTDBG       =
*      I_APPLOGPARENT    =
*      I_GRAPHICSPARENT  =
*      I_NAME            =
*      I_FCAT_COMPLETE   = SPACE
*    EXCEPTIONS
*      ERROR_CNTL_CREATE = 1
*      ERROR_CNTL_INIT   = 2
*      ERROR_CNTL_LINK   = 3
*      ERROR_DP_CREATE   = 4
*      OTHERS            = 5
       .
   IF SY-SUBRC = 0.
* Display ALV
     CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
*    EXPORTING
*      I_BUFFER_ACTIVE               =
*      I_BYPASSING_BUFFER            =
*      I_CONSISTENCY_CHECK           =
*      I_STRUCTURE_NAME              =
*      IS_VARIANT                    =
*      I_SAVE                        =
*      I_DEFAULT                     = 'X'
*      IS_LAYOUT                     =
*      IS_PRINT                      =
*      IT_SPECIAL_GROUPS             =
*      IT_TOOLBAR_EXCLUDING          =
*      IT_HYPERLINK                  =
*      IT_ALV_GRAPHICS               =
*      IT_EXCEPT_QINFO               =
*      IR_SALV_ADAPTER               =
       CHANGING
         IT_OUTTAB                     = T_INFILE
         IT_FIELDCATALOG               = T_FIELDCAT
*      IT_SORT                       =
*      IT_FILTER                     =
*    EXCEPTIONS
*      INVALID_PARAMETER_COMBINATION = 1
*      PROGRAM_ERROR                 = 2
*      TOO_MANY_LINES                = 3
*      OTHERS                        = 4
             .
     IF SY-SUBRC <> 0.
*   Implement suitable error handling here
     ENDIF.
     CALL SCREEN 9001.
   ENDIF.

 ENDIF.

ENDFORM.

FORM SET_FIELDCAT .

 W_FIELDCAT-FIELDNAME = 'TICKER'.
 T_FIELDCAT = VALUE #( ( FIELDNAME = 'TICKER' TABNAME = 'T_INFILE')
                       ( FIELDNAME = 'DATE'   TABNAME = 'T_INFILE')
                      ) .

ENDFORM.



No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...