메뉴 건너뛰기

SAP 한국 커뮤니티



REPORT z_demo_alv_event_exit_2

노름마치 2007.11.05 21:57 조회 수 : 2960

REPORT z_demo_alv_event_exit_2.
*>*********************************************************************
* This report reads and displays data from table VBAK                 *
* using the FM : REUSE_ALV_GRID_DISPLAY                               *
* The columns are displayed in the same order than the Sort Order     *
* There is an underline and subtotal if the sort is by VKORG or KUNNR *
*---------------------------------------------------------------------*
* Author : Michel PIOUD                                               *
* Email : mpioud@yahoo.fr  HomePage : http://www.geocities.com/mpioud *
*>*********************************************************************


TABLES : vbak.                         " Sales Document: Header Data


TYPE-POOLS: slis.                      " ALV Global Types


SELECT-OPTIONS :
  s_vkorg FOR vbak-vkorg,              " Sales organization
  s_kunnr FOR vbak-kunnr,              " Sold-to party
  s_vbeln FOR vbak-vbeln.              " Sales document


SELECTION-SCREEN :
  SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max.
PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.
SELECTION-SCREEN END OF LINE.


DATA:
  BEGIN OF gt_vbak OCCURS 0,
    vkorg LIKE vbak-vkorg,             " Sales organization
    kunnr LIKE vbak-kunnr,             " Sold-to party
    vbeln LIKE vbak-vbeln,             " Sales document
    netwr LIKE vbak-netwr,             " Net Value of the Sales Order
    waerk LIKE vbak-waerk,             " SD document currency
  END OF gt_vbak.


*---------------------------------------------------------------------*
INITIALIZATION.


  v_1 = 'Maximum of records to read'.


*---------------------------------------------------------------------*
START-OF-SELECTION.


  PERFORM f_read_data.


  PERFORM f_display_data.


*---------------------------------------------------------------------*
*      Form  f_read_data
*---------------------------------------------------------------------*
FORM f_read_data.


  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbak
           FROM vbak
             UP TO p_max ROWS
          WHERE kunnr IN s_kunnr
            AND vbeln IN s_vbeln
            AND vkorg IN s_vkorg.


ENDFORM.                               " F_READ_DATA
*---------------------------------------------------------------------*
*      Form  f_display_data
*---------------------------------------------------------------------*
FORM f_display_data.


* Macro definition
  DEFINE m_fieldcat.
    add 1 to ls_fieldcat-col_pos.
    ls_fieldcat-fieldname   = &1.
    ls_fieldcat-ref_tabname = 'VBAK'.
    ls_fieldcat-do_sum      = &2.
    ls_fieldcat-cfieldname  = &3.
    append ls_fieldcat to lt_fieldcat.
  END-OF-DEFINITION.


  DEFINE m_sort.
    add 1 to ls_sort-spos.
    ls_sort-fieldname = &1.
    ls_sort-up = 'X'.
    append ls_sort to lt_sort.
  END-OF-DEFINITION.


  DATA:
    ls_fieldcat   TYPE slis_fieldcat_alv,
    lt_fieldcat   TYPE slis_t_fieldcat_alv,
    lt_sort       TYPE slis_t_sortinfo_alv,
    ls_sort       TYPE slis_sortinfo_alv,
    ls_layout     TYPE slis_layout_alv,
    lt_event_exit TYPE slis_t_event_exit,
    ls_event_exit TYPE slis_event_exit.
*
  DEFINE m_event_exit.
    clear ls_event_exit.
    ls_event_exit-ucomm = &1.
    ls_event_exit-after = 'X'.
    append ls_event_exit to lt_event_exit.
  END-OF-DEFINITION.


* Build Event Exit Table
  m_event_exit '&OUP'.                 " Sort up
  m_event_exit '&ODN'.                 " Sort Down


  m_fieldcat 'VBELN' ''  ''.
  m_fieldcat 'VKORG' ''  ''.
  m_fieldcat 'KUNNR' ''  ''.
  m_fieldcat 'NETWR' 'X' 'WAERK'.
  m_fieldcat 'WAERK' ''  ''.


  m_sort 'VBELN'.                      " Sort by vbeln


  ls_layout-colwidth_optimize = 'X'.
  ls_layout-zebra             = 'X'.
  ls_layout-cell_merge        = 'X'.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = sy-cprog
            i_callback_user_command = 'USER_COMMAND'
            is_layout               = ls_layout
            it_fieldcat             = lt_fieldcat
            it_sort                 = lt_sort
            it_event_exit           = lt_event_exit
       TABLES
            t_outtab                = gt_vbak.


ENDFORM.                               " F_DISPLAY_DATA
*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM user_command USING u_ucomm     TYPE syucomm
                        us_selfield TYPE slis_selfield.     "#EC CALLED


  CASE u_ucomm.
    WHEN '&OUP' OR '&ODN'.             " Sort
      PERFORM f_modif_fieldcat.
      us_selfield-refresh = 'X'.
  ENDCASE.


ENDFORM.                               " USER_COMMAND
*---------------------------------------------------------------------*
*       Form  F_MODIF_FIELDCAT
*---------------------------------------------------------------------*
FORM f_modif_fieldcat.


  DATA:
    lt_fieldcat TYPE slis_t_fieldcat_alv,
    lt_sort     TYPE slis_t_sortinfo_alv.


  FIELD-SYMBOLS :
    <sort>     TYPE slis_sortinfo_alv,
    <fieldcat> TYPE slis_fieldcat_alv.


* Read current ALV list information
  CALL FUNCTION 'REUSE_ALV_GRID_LAYOUT_INFO_GET'
       IMPORTING
            et_fieldcat   = lt_fieldcat
            et_sort       = lt_sort
       EXCEPTIONS
            no_infos      = 1
            program_error = 2
            OTHERS        = 3.


  IF sy-subrc NE 0.
    EXIT.
  ENDIF.


  CHECK NOT lt_sort[] IS INITIAL.


* Fieldcat modification
  DESCRIBE TABLE lt_fieldcat.


  LOOP AT lt_fieldcat ASSIGNING <fieldcat>.
    <fieldcat>-col_pos = <fieldcat>-col_pos + sy-tfill.
  ENDLOOP.


  LOOP AT lt_fieldcat ASSIGNING <fieldcat>.
    READ TABLE lt_sort ASSIGNING <sort>
                        WITH KEY fieldname = <fieldcat>-fieldname.
    CHECK sy-subrc EQ 0.
    <fieldcat>-col_pos = <sort>-spos.
  ENDLOOP.


  SORT lt_fieldcat BY col_pos.
  LOOP AT lt_fieldcat ASSIGNING <fieldcat>.
    <fieldcat>-col_pos = sy-tabix.
  ENDLOOP.


* Underline and subtotal if the sort is by KUNNR or VKORG
  READ TABLE lt_sort ASSIGNING <sort> INDEX 1.
  IF <sort>-fieldname = 'KUNNR'  OR
     <sort>-fieldname = 'VKORG'.
    <sort>-group = 'UL'.               " Underline
    <sort>-subtot = 'X'.               " Subtotal
  ENDIF.


  CALL FUNCTION 'REUSE_ALV_GRID_LAYOUT_INFO_SET'
       EXPORTING
            it_sort     = lt_sort
            it_fieldcat = lt_fieldcat.


ENDFORM.                               " F_MODIF_FIELDCAT
******** END OF PROGRAM Z_DEMO_ALV_EVENT_EXIT_2 ***********************