Saltar al contenido

Ejemplo de programa ALV usando REUSE_ALV_GRID_DISPLAY

Un Usuario hizo la siguiente pregunta

Hola,

Quiero un programa alv de muestra usando REUSE_ALV_GRID_DISPLAY.

Quiero saber cómo incluir el catálogo de campo y el diseño …

¡Muchos gracias!

4 respuestas

  • Ex miembro

    Hola Mark: Mira el tablero de muestra para poner el texto de la forma en que solicitaste un encabezado ALV. Controlar parte superior de la página salida.

    REPORT  zvenkat_alv_grid.
    TABLES:t001.
    "Types
    TYPES:
          BEGIN OF t_1001,
            bukrs TYPE t001-bukrs,
            butxt TYPE t001-butxt,
            ort01 TYPE t001-ort01,
            land1 TYPE t001-land1,
          END OF t_1001.
    "Work area
    DATA:
          w_t001 TYPE t_1001.
    "Internal table
    DATA:
          i_t001 TYPE STANDARD TABLE OF t_1001.
    
    *&---------------------------------------------------------------------*
    * 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_events.
      PERFORM build_layout.
      PERFORM list_display.
    *&---------------------------------------------------------------------*
    *&      Form  get_data
    *&---------------------------------------------------------------------*
    FORM get_data .
    
      SELECT bukrs
             butxt
             ort01
             land1
        FROM t001
        INTO TABLE i_t001
        UP TO 30 ROWS.
    
    ENDFORM.                    " get_data
    *&---------------------------------------------------------------------*
    *&      Form  build_fieldcatlog
    *&---------------------------------------------------------------------*
    FORM build_fieldcatlog .
      CLEAR:w_fieldcat,i_fieldcat[].
    
      PERFORM build_fcatalog USING:
               'BUKRS' 'I_T001' 'BUKRS',
               'BUTXT' 'I_T001' 'BUTXT',
               'ORT01' 'I_T001' 'ORT01',
               'LAND1' 'I_T001' 'LAND1'.
    
    ENDFORM.                    "BUILD_FIELDCATLOG
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_FCATALOG
    *&---------------------------------------------------------------------*
    FORM build_fcatalog USING l_field l_tab l_text.
    
      w_fieldcat-fieldname      = l_field.
      w_fieldcat-tabname        = l_tab.
      w_fieldcat-seltext_m      = l_text.
    
      APPEND w_fieldcat TO i_fieldcat.
      CLEAR w_fieldcat.
    
    ENDFORM.                    " build_fieldcatlog
    *&---------------------------------------------------------------------*
    *&      Form  build_events
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    FORM build_events.
      CLEAR :
            w_events, i_events[].
      w_events-name="TOP_OF_PAGE"."Event Name
      w_events-form = 'TOP_OF_PAGE'."Callback event subroutine
      APPEND w_events TO i_events.
      CLEAR  w_events.
    
    ENDFORM.                    "build_events
    *&---------------------------------------------------------------------*
    *&      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_GRID_DISPLAY'
        EXPORTING
          i_callback_program = l_program
          is_layout          = w_layout
          it_fieldcat        = i_fieldcat
          it_events          = i_events
        TABLES
          t_outtab           = i_t001
        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
    *&---------------------------------------------------------------------*
    *&      Form  top_of_page
    *&---------------------------------------------------------------------*
    FORM top_of_page.
      DATA :
       li_header TYPE slis_t_listheader,
       w_header  LIKE LINE OF li_header.
      DATA:
            l_date TYPE char10.
      WRITE sy-datum TO l_date.
      w_header-typ  = 'H'.
      CONCATENATE sy-repid ':' 'From Date' l_date INTO w_header-info SEPARATED BY space.
      APPEND w_header TO li_header.
      CLEAR w_header.
    
      w_header-typ  = 'S'.
      w_header-info = sy-title.
      APPEND w_header TO li_header.
      CLEAR w_header.
    
      w_header-typ  = 'A'.
      w_header-info = sy-uname.
      APPEND w_header TO li_header.
      CLEAR w_header.
    
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          it_list_commentary = li_header.
    
    ENDFORM.                    "top_of_page

    Espero que te ayude. Acerca de Venkat.O

  • Ex miembro

    Hola, mira el programa de muestra.

    *REPORT  ZALV_REPORT_SFLIGHT.
    TABLES : SFLIGHT.
    TYPE-POOLS : SLIS.**INTERNAL TABLE DECLARTION
    DATA : WA_SFLIGHT TYPE SFLIGHT,
           IT_SFLIGHT TYPE TABLE OF SFLIGHT.**DATA DECLARTION
    DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
          GD_LAYOUT    TYPE SLIS_LAYOUT_ALV,
          GD_REPID     LIKE SY-REPID,
          G_SAVE TYPE C VALUE 'X',
          G_VARIANT TYPE DISVARIANT,
          GX_VARIANT TYPE DISVARIANT,
          G_EXIT TYPE C,
          ISPFLI TYPE TABLE OF SPFLI.* To understand the importance of the following parameter, click here.
    **SELECTION SCREEN DETAILS
    SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002 .
    PARAMETERS: VARIANT LIKE DISVARIANT-VARIANT.
    SELECTION-SCREEN END OF BLOCK B1.
    **GETTING DEFAULT VARIANT
    INITIALIZATION.
      GX_VARIANT-REPORT = SY-REPID.
      CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
        EXPORTING
          I_SAVE     = G_SAVE
        CHANGING
          CS_VARIANT = GX_VARIANT
        EXCEPTIONS
          NOT_FOUND  = 2.
      IF SY-SUBRC = 0.
        VARIANT = GX_VARIANT-VARIANT.
      ENDIF.**PERFORM DECLARATIONS
    START-OF-SELECTION.
      PERFORM DATA_RETRIVEL.
      PERFORM BUILD_FIELDCATALOG.
      PERFORM DISPLAY_ALV_REPORT.
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_FIELDCATALOG
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM BUILD_FIELDCATALOG .  FIELDCATALOG-FIELDNAME   = 'CARRID'.
      FIELDCATALOG-SELTEXT_M   = 'Airline Code'.
      FIELDCATALOG-COL_POS     = 0.
      APPEND FIELDCATALOG TO FIELDCATALOG.
      CLEAR  FIELDCATALOG.
      FIELDCATALOG-FIELDNAME   = 'CONNID'.
      FIELDCATALOG-SELTEXT_M   = 'Flight Connection Number'.
      FIELDCATALOG-COL_POS     = 1.
      APPEND FIELDCATALOG TO FIELDCATALOG.
      CLEAR  FIELDCATALOG.  FIELDCATALOG-FIELDNAME   = 'FLDATE'.
      FIELDCATALOG-SELTEXT_M   = 'Flight date'.
      FIELDCATALOG-COL_POS     = 2.
      APPEND FIELDCATALOG TO FIELDCATALOG.
      CLEAR  FIELDCATALOG.  FIELDCATALOG-FIELDNAME   = 'PRICE'.
      FIELDCATALOG-SELTEXT_M   = 'Airfare'.
      FIELDCATALOG-COL_POS     = 3.
      FIELDCATALOG-OUTPUTLEN   = 20.
      APPEND FIELDCATALOG TO FIELDCATALOG.
      CLEAR  FIELDCATALOG.
    ENDFORM.                    " BUILD_FIELDCATALOG
    
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_ALV_REPORT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM DISPLAY_ALV_REPORT .
      GD_REPID = SY-REPID.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          I_CALLBACK_PROGRAM      = GD_REPID
          I_CALLBACK_TOP_OF_PAGE  = 'TOP-OF-PAGE'  "see FORM
          I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
          IT_FIELDCAT             = FIELDCATALOG[]
          I_SAVE                  = 'X'
          IS_VARIANT              = G_VARIANT
        TABLES
          T_OUTTAB                = IT_SFLIGHT
        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.                    "DISPLAY_ALV_REPORT" DISPLAY_ALV_REPORT
    *&---------------------------------------------------------------------*
    *&      Form  DATA_RETRIVEL
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM DATA_RETRIVEL .
      SELECT * FROM SFLIGHT INTO TABLE IT_SFLIGHT.
    ENDFORM.                    " DATA_RETRIVEL*-------------------------------------------------------------------*
    * Form  TOP-OF-PAGE                                                 *
    *-------------------------------------------------------------------*
    * ALV Report Header                                                 *
    *-------------------------------------------------------------------*
    FORM TOP-OF-PAGE.
    *ALV Header declarations
      DATA: T_HEADER TYPE SLIS_T_LISTHEADER,
            WA_HEADER TYPE SLIS_LISTHEADER,
            T_LINE LIKE WA_HEADER-INFO,
            LD_LINES TYPE I,
            LD_LINESC(10) TYPE C.* Title
      WA_HEADER-TYP  = 'H'.
      WA_HEADER-INFO = 'SFLIGHT Table Report'.
      APPEND WA_HEADER TO T_HEADER.
      CLEAR WA_HEADER.* Date
      WA_HEADER-TYP  = 'S'.
      WA_HEADER-KEY = 'Date: '.
      CONCATENATE  SY-DATUM+6(2) '.'
                   SY-DATUM+4(2) '.'
                   SY-DATUM(4) INTO WA_HEADER-INFO.   "todays date
      APPEND WA_HEADER TO T_HEADER.
      CLEAR: WA_HEADER.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          IT_LIST_COMMENTARY = T_HEADER.
    ENDFORM.                    "top-of-page

    Espero que te ayude. Saludos, Venakt.O

  • Ex miembro

    Hola,

    Pruébalo así

    TABLES:     ekko.
    
    TYPE-POOLS: slis.                                 "ALV Declarations
    
    *Data Declaration
    *----------------
    TYPES: BEGIN OF t_ekko,
      ebeln TYPE ekpo-ebeln,
      ebelp TYPE ekpo-ebelp,
      statu TYPE ekpo-statu,
      aedat TYPE ekpo-aedat,
      matnr TYPE ekpo-matnr,
      menge TYPE ekpo-menge,
      meins TYPE ekpo-meins,
      netpr TYPE ekpo-netpr,
      peinh TYPE ekpo-peinh,
      line_color(4) TYPE c,     "Used to store row color attributes
     END OF t_ekko.
    
    DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
          wa_ekko TYPE t_ekko.
    
    *ALV data declarations
    DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
          gd_tab_group TYPE slis_t_sp_group_alv,
          gd_layout    TYPE slis_layout_alv,
          gd_repid     LIKE sy-repid.
    
    
    DATA : t TYPE slis_t_sp_group_alv .
    ************************************************************************
    *Start-of-selection.
    START-OF-SELECTION.
    
      PERFORM data_retrieval.
      PERFORM build_fieldcatalog.
      PERFORM build_layout.
      PERFORM display_alv_report.
    
    
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_FIELDCATALOG
    *&---------------------------------------------------------------------*
    *       Build Fieldcatalog for ALV Report
    *----------------------------------------------------------------------*
    FORM build_fieldcatalog.
    
      fieldcatalog-fieldname="EBELN".
      fieldcatalog-seltext_m   = 'Purchase Order'.
      fieldcatalog-col_pos     = 0.
      fieldcatalog-outputlen   = 10.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="EBELP".
      fieldcatalog-seltext_m   = 'PO Item'.
      fieldcatalog-col_pos     = 1.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="STATU".
      fieldcatalog-seltext_m   = 'Status'.
      fieldcatalog-col_pos     = 2.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="AEDAT".
      fieldcatalog-seltext_m   = 'Item change date'.
      fieldcatalog-col_pos     = 3.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="MATNR".
      fieldcatalog-seltext_m   = 'Material Number'.
      fieldcatalog-col_pos     = 4.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="MENGE".
      fieldcatalog-seltext_m   = 'PO quantity'.
      fieldcatalog-col_pos     = 5.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="MEINS".
      fieldcatalog-seltext_m   = 'Order Unit'.
      fieldcatalog-col_pos     = 6.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="NETPR".
      fieldcatalog-seltext_m   = 'Net Price'.
      fieldcatalog-col_pos     = 7.
      fieldcatalog-outputlen   = 15.
      fieldcatalog-datatype="CURR".
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    
      fieldcatalog-fieldname="PEINH".
      fieldcatalog-seltext_m   = 'Price Unit'.
      fieldcatalog-col_pos     = 8.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    ENDFORM.                    " BUILD_FIELDCATALOG
    
    
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_LAYOUT
    *&---------------------------------------------------------------------*
    *       Build layout for ALV grid report
    *----------------------------------------------------------------------*
    FORM build_layout.
    
      gd_layout-no_input="X".
      gd_layout-colwidth_optimize="X".
      gd_layout-zebra="X".
    *  gd_layout-info_fieldname="LINE_COLOR".
    *  gd_layout-def_status="A".
    
    ENDFORM.                    " BUILD_LAYOUT
    
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_ALV_REPORT
    *&---------------------------------------------------------------------*
    *       Display report using ALV grid
    *----------------------------------------------------------------------*
    FORM display_alv_report.
      gd_repid = sy-repid.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program = gd_repid
          is_layout          = gd_layout
          it_fieldcat        = fieldcatalog[]
          i_save="X"
        TABLES
          t_outtab           = it_ekko
        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.                    " DISPLAY_ALV_REPORT
    
    
    *&---------------------------------------------------------------------*
    *&      Form  DATA_RETRIEVAL
    *&---------------------------------------------------------------------*
    *       Retrieve data form EKPO table and populate itab it_ekko
    *----------------------------------------------------------------------*
    FORM data_retrieval.
      DATA: ld_color(1) TYPE c.
    
      SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
       UP TO 10 ROWS
        FROM ekpo
        INTO TABLE it_ekko.
    
    ENDFORM.                    " DATA_RETRIEVAL
    

    recompensa si es útil.