메뉴 건너뛰기

SAP 한국 커뮤니티



REPORT z_demo_3_alv_list

노름마치 2007.11.05 21:56 조회 수 : 3295

REPORT z_demo_3_alv_list.
*---------------------------------------------------------------------*
* This program is an example with 3 ALV Grid lists                    *
* The customers are displayed in the first list                       *
* When a line is selected, the customer's orders are displayed in     *
* the second list                                                     *
* When a line is selected in the second list, the items orders are    *
* displayed in the second list                                        *
*---------------------------------------------------------------------*
* Author : Michel PIOUD                                               *
* Email : mpioud@yahoo.fr  HomePage : http://www.geocities.com/mpioud *
*---------------------------------------------------------------------*
* Macro definition
DEFINE m_fieldcat.
  add 1 to ls_fieldcat-col_pos.
  ls_fieldcat-fieldname   = &1.
  ls_fieldcat-ref_tabname = &2.
  append ls_fieldcat to lt_fieldcat.
END-OF-DEFINITION.


TYPE-POOLS: slis.                      " ALV Global types


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


DATA:
* Data displayed in the first list
  BEGIN OF gt_kna1 OCCURS 0,
    kunnr LIKE kna1-kunnr,             " Customer number
    name1 LIKE kna1-name1,             " Customer name
    ort01 LIKE kna1-ort01,             " Customer city
  END OF gt_kna1,


* Data displayed in the second list
  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
  END OF gt_vbak,


* Data displayed in the third list
  BEGIN OF gt_vbap OCCURS 0,
    vbeln  LIKE vbap-vbeln,            " Sales document
    posnr  LIKE vbap-posnr,            " Sales document item
    matnr  LIKE vbap-matnr,            " Material number
    arktx  LIKE vbap-arktx,            " Short text for sales order item
    kwmeng LIKE vbap-kwmeng,           " Order quantity
    netwr  LIKE vbap-netwr,            " Net value of the order item
  END OF gt_vbap.


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


  v_1 = 'Maximum of records to read'.


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


  PERFORM f_read_data_kna1.


  PERFORM f_display_data_kna1.


*---------------------------------------------------------------------*
*      Form  f_read_data_kna1
*---------------------------------------------------------------------*
FORM f_read_data_kna1.


* Read customer data with a least one order
  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_kna1
           FROM kna1 AS k
             UP TO p_max ROWS
          WHERE EXISTS
       ( SELECT kunnr FROM vbak WHERE kunnr = k~kunnr ).


ENDFORM.                               " F_READ_DATA_KNA1
*---------------------------------------------------------------------*
*      Form  f_display_data_kna1
*---------------------------------------------------------------------*
FORM f_display_data_kna1.


  DATA:
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv.


* Build the field catalog
  m_fieldcat 'KUNNR' 'KNA1'.
  m_fieldcat 'NAME1' 'KNA1'.
  m_fieldcat 'ORT01' 'KNA1'.


* Display the first list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = sy-cprog
            i_callback_user_command = 'USER_COMMAND_KNA1'
            it_fieldcat             = lt_fieldcat
       TABLES
            t_outtab                = gt_kna1.


ENDFORM.                               " F_DISPLAY_DATA_KNA1
*---------------------------------------------------------------------*
*       FORM USER_COMMAND_KNA1                                        *
*---------------------------------------------------------------------*
FORM user_command_kna1 USING u_ucomm     LIKE sy-ucomm
                             us_selfield TYPE slis_selfield."#EC CALLED


  CASE u_ucomm.
    WHEN '&IC1'.
      READ TABLE gt_kna1 INDEX us_selfield-tabindex.
      CHECK sy-subrc EQ 0.
      PERFORM f_read_data_vbak.        " Read data from VBAK
      PERFORM f_display_data_vbak.     " Display orders
  ENDCASE.


ENDFORM.                               " USER_COMMAND_KNA1
*---------------------------------------------------------------------*
*      Form  f_read_data_vbak
*---------------------------------------------------------------------*
FORM f_read_data_vbak.


  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbak
           FROM vbak
             UP TO p_max ROWS
          WHERE kunnr = gt_kna1-kunnr.


ENDFORM.                               " F_READ_DATA_VBAK
*---------------------------------------------------------------------*
*      Form  f_display_data_vbak
*---------------------------------------------------------------------*
FORM f_display_data_vbak.


  DATA:
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv.


* Build the field catalog
  m_fieldcat 'VKORG' 'VBAK'.
  m_fieldcat 'KUNNR' 'VBAK'.
  m_fieldcat 'VBELN' 'VBAK'.
  m_fieldcat 'NETWR' 'VBAK'.


* Display the second list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = sy-cprog
            i_callback_user_command = 'USER_COMMAND_VBAK'
            it_fieldcat             = lt_fieldcat
       TABLES
            t_outtab                = gt_vbak.


ENDFORM.                               " F_DISPLAY_DATA_VBAK
*---------------------------------------------------------------------*
*       FORM USER_COMMAND_VBAK                                        *
*---------------------------------------------------------------------*
FORM user_command_vbak USING u_ucomm     LIKE sy-ucomm
                             us_selfield TYPE slis_selfield."#EC CALLED


  CASE u_ucomm.
    WHEN '&IC1'.
      READ TABLE gt_vbak INDEX us_selfield-tabindex.
      CHECK sy-subrc EQ 0.
      PERFORM f_read_data_vbap.        " Read data from VBAP
      PERFORM f_display_data_vbap.     " Display items
  ENDCASE.


ENDFORM.                               " USER_COMMAND_VBAK
*---------------------------------------------------------------------*
*      Form  f_read_data_vbap
*---------------------------------------------------------------------*
FORM f_read_data_vbap.


  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbap
           FROM vbap
          WHERE vbeln = gt_vbak-vbeln.


ENDFORM.                               " F_READ_DATA_VBAP
*---------------------------------------------------------------------*
*      Form  f_display_data_vbap
*---------------------------------------------------------------------*
FORM f_display_data_vbap.


  DATA:
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv.


* Build the field catalog
  m_fieldcat 'VBELN'  'VBAP'.
  m_fieldcat 'POSNR'  'VBAP'.
  m_fieldcat 'MATNR'  'VBAP'.
  m_fieldcat 'ARKTX'  'VBAP'.
  m_fieldcat 'KWMENG' 'VBAP'.
  m_fieldcat 'NETWR'  'VBAP'.


* Display the third list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            it_fieldcat = lt_fieldcat
       TABLES
            t_outtab    = gt_vbap.


ENDFORM.                               " F_DISPLAY_DATA_VBAP
***************** END OF PROGRAM Z_DEMO_3_ALV_LIST ******************