Friday, September 6, 2019

Object Oriented ALV using Field Symbol

TABLES ekpo.
FIELD-SYMBOLS<it_ekpo>   TYPE STANDARD TABLE,
               <it_out>    TYPE STANDARD TABLE,
               <it_fcat>   TYPE STANDARD TABLE,
               <wa_layout> TYPE lvc_s_layo.
DATAobj_con TYPE REF TO cl_gui_custom_container,
      obj_alv TYPE REF TO cl_gui_alv_grid.

INITIALIZATION.
  SELECT-OPTIONS s_ebeln FOR ekpo-ebeln.

CLASS pur DEFINITION.
  PUBLIC SECTION.
    TYPESBEGIN OF ts_ekpo,
             ebeln TYPE ekpo-ebeln,
             ebelp TYPE ekpo-ebelp,
             txz01 TYPE ekpo-txz01,
             ktmng TYPE ekpo-ktmng,
             meins TYPE ekpo-meins,
             netpr TYPE ekpo-netpr,
             peinh TYPE ekpo-peinh,
           END OF ts_ekpo.
    DATA dref TYPE REF TO data.
    METHODSekpooutputfield_catalog.
ENDCLASS.

CLASS pur IMPLEMENTATION.
  METHOD ekpo.
    CREATE DATA dref TYPE TABLE OF ts_ekpo.
    ASSIGN dref->TO <it_ekpo>.

    SELECT ebeln ebelp txz01
           ktmng meins netpr peinh
      FROM ekpo INTO TABLE <it_ekpo>
      WHERE ebeln IN s_ebeln.
  ENDMETHOD.

  METHOD output.
    FIELD-SYMBOLS<wa_out>  TYPE ts_ekpo,
                   <wa_ekpo> TYPE ts_ekpo.

    IF <it_ekpo> IS ASSIGNED.
      CREATE DATA dref TYPE ts_ekpo.
      ASSIGN dref->TO <wa_out>.
      CREATE DATA dref TYPE TABLE OF ts_ekpo.
      ASSIGN dref->TO <it_out>.

      LOOP AT <it_ekpo> ASSIGNING <wa_ekpo>.

        AT NEW ('EBELN').
          <wa_out>-ebeln <wa_ekpo>-ebeln.
        ENDAT.

        <wa_out>-ebelp <wa_ekpo>-ebelp.
        <wa_out>-txz01 <wa_ekpo>-txz01.
        <wa_out>-ktmng <wa_ekpo>-ktmng.
        <wa_out>-meins <wa_ekpo>-meins.
        <wa_out>-netpr <wa_ekpo>-netpr.
        <wa_out>-peinh <wa_ekpo>-peinh.

        APPEND <wa_out> TO <it_out>.
      ENDLOOP.
    ENDIF.
  ENDMETHOD.

  METHOD field_catalog.
    DATA lv_col TYPE VALUE IS INITIAL.
    FIELD-SYMBOLS <wa_fcat> TYPE lvc_s_fcat.
    CREATE DATA dref TYPE lvc_s_fcat.
    ASSIGN dref->TO <wa_fcat>.

    CREATE DATA dref TYPE TABLE OF lvc_s_fcat.
    ASSIGN dref->TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'EBELN'.
    <wa_fcat>-reptext   'Purchase Order'.
    <wa_fcat>-no_zero   'X'.
    APPEND <wa_fcat> TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'EBELP'.
    <wa_fcat>-reptext   'Item'.
    APPEND <wa_fcat> TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'TXZ01'.
    <wa_fcat>-reptext   'Description'.
    APPEND <wa_fcat> TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'KTMNG'.
    <wa_fcat>-reptext   'Quantity'.
    APPEND <wa_fcat> TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'MEINS'.
    <wa_fcat>-reptext   'UoM'.
    APPEND <wa_fcat> TO <it_fcat>.

    lv_col lv_col + 1.
    <wa_fcat>-col_pos   lv_col.
    <wa_fcat>-fieldname 'NETPR'.
    <wa_fcat>-reptext   'Price'.
    APPEND <wa_fcat> TO <it_fcat>.

    CREATE DATA dref TYPE lvc_s_layo.
    ASSIGN dref->TO <wa_layout>.
    <wa_layout>-zebra      'X'.
    <wa_layout>-cwidth_opt 'X'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA po TYPE REF TO pur.
  CREATE OBJECT po.
  SET SCREEN 0100.
  CALL METHODpo->ekpo,
               po->output,
               po->field_catalog.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'PF_100'.
  SET TITLEBAR 'T_100'.

  IF <wa_layout> IS ASSIGNED AND
     <it_out>    IS ASSIGNED AND
     <it_fcat>   IS ASSIGNED.

    IF obj_con IS INITIAL.
      CREATE OBJECT obj_con
        EXPORTING
          container_name              'ZSR_TEST'
        EXCEPTIONS
          cntl_error                  1
          cntl_system_error           2
          create_error                3
          lifetime_error              4
          lifetime_dynpro_dynpro_link 5
          OTHERS                      6.
    ENDIF.

    CREATE OBJECT obj_alv
      EXPORTING
        i_parent          obj_con
      EXCEPTIONS
        error_cntl_create 1
        error_cntl_init   2
        error_cntl_link   3
        error_dp_create   4
        OTHERS            5.

    CALL METHOD obj_alv->set_table_for_first_display
      EXPORTING
        i_save                        'A'
        i_default                     'X'
        is_layout                     <wa_layout>
      CHANGING
        it_outtab                     <it_out>
        it_fieldcatalog               <it_fcat>
      EXCEPTIONS
        invalid_parameter_combination 1
        program_error                 2
        too_many_lines                3
        OTHERS                        4.
  ENDIF.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.

ENDMODULE.


No comments: