关键点:
1、 TYPE-POOLS slis. " ALV Global types
2、 l_rec_keyinfo的正确设置
3、 it_fieldcat设置中的细节点(代码中的三个前个子段必须)
4、 CHECKBOX功能(内表中必须定义CHECK字段)
5、 EXPAND功能(expand TYPE xfeld 字段定义不能缺少,
l_rec_layout-expand_fieldname = 'EXPAND'.'EXPAND'必须大写)
以下是代码部分:
REPORT z_alv NO STANDARD PAGE HEADING.
*-----------------------------------------------------------------------
* T Y P E S - P O O L
*-----------------------------------------------------------------------
TYPE-POOLS slis. " ALV Global types
*-----------------------------------------------------------------------
* T Y P E S
*-----------------------------------------------------------------------
TYPES :
* Head data Table Types
BEGIN OF typ_vbak,
vbeln TYPE vbak-vbeln, " Sales document
kunnr TYPE vbak-kunnr, " Sold-to party
netwr TYPE vbak-netwr, " Net Value of the Sales Order
waerk TYPE vbak-waerk, " SD document currency
erdat TYPE vbak-erdat, " Creation date
expand TYPE xfeld,
END OF typ_vbak,
* Items data Table Types
BEGIN OF typ_vbap,
vbeln TYPE vbap-vbeln, " Sales document
posnr TYPE vbap-posnr, " Sales document
matnr TYPE vbap-matnr, " Material number
arktx TYPE vbap-arktx, " Material description
netwr TYPE vbap-netwr, " Net Value of the Sales Order
waerk TYPE vbap-waerk, " SD document currency
check TYPE c, " Check field
END OF typ_vbap.
*-----------------------------------------------------------------------
* D A T A S
*-----------------------------------------------------------------------
*-Internal talbe data
DATA:
* Head data table
tbl_vbak TYPE STANDARD TABLE OF typ_vbak,
* Item data table
tbl_vbap TYPE STANDARD TABLE OF typ_vbap.
**-Global data definition
*DATA:
** Head data table
* tbl_vbak TYPE STANDARD TABLE OF typ_vbak,
** Item data table
* tbl_vbap TYPE STANDARD TABLE OF typ_vbap.
*-----------------------------------------------------------------------
* C O N S T A N T S
*-----------------------------------------------------------------------
CONSTANTS:
cns_vbak TYPE slis_tabname VALUE 'tbl_vbak',
cns_vbap TYPE slis_tabname VALUE 'tbl_vbap'.
*-----------------------------------------------------------------------
* P A R A M E T E R S & S E L E C T - O P T I O N S
*-----------------------------------------------------------------------
*-> Max data to read
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 2(30) txt01 FOR FIELD p_max.
PARAMETERS p_max(2) TYPE n.
SELECTION-SCREEN COMMENT 37(10) txt04.
SELECTION-SCREEN END OF LINE.
*-> With Expand
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 2(30) txt02 FOR FIELD p_exp.
PARAMETERS p_exp AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
*-> With Check Box
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 2(30) txt03 FOR FIELD p_chk.
PARAMETERS p_chk AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
*-----------------------------------------------------------------------
* I N I T I A L I Z A T I O N
*-----------------------------------------------------------------------
INITIALIZATION.
txt01 = 'Maximum of records to select'.
txt02 = 'Expand needed'.
txt03 = 'CheckBox needed'.
txt04 = 'Default 10'.
*-----------------------------------------------------------------------
* S T A R T - O F - S E L E C T I O N
*-----------------------------------------------------------------------
START-OF-SELECTION.
*-> Select data
PERFORM select_data.
*-----------------------------------------------------------------------
* E N D - O F - S E L E C T I O N
*-----------------------------------------------------------------------
END-OF-SELECTION.
*-> Edit data
PERFORM edit_alv_data.
*-> Display data
PERFORM display_alv.
*&---------------------------------------------------------------------*
*& Form select_data
*&---------------------------------------------------------------------*
* select_data
*----------------------------------------------------------------------*
FORM select_data .
* Read Sales Document: Header Data
IF p_max IS INITIAL.
p_max = 10.
ENDIF.
SELECT vbeln
kunnr
netwr
waerk
erdat
FROM vbak
UP TO p_max ROWS
INTO TABLE tbl_vbak.
IF tbl_vbak IS NOT INITIAL.
* Read Sales Document: Item Data
SELECT vbeln
posnr
matnr
arktx
netwr
waerk
FROM vbap
INTO TABLE tbl_vbap
FOR ALL ENTRIES IN tbl_vbak
WHERE vbeln = tbl_vbak-vbeln.
ENDIF.
ENDFORM. " select_data
*&---------------------------------------------------------------------*
*& Form edit_alv_data
*&---------------------------------------------------------------------*
* Edit data
*----------------------------------------------------------------------*
FORM edit_alv_data .
ENDFORM. " edit_alv_data
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* Display data
*----------------------------------------------------------------------*
FORM display_alv .
* Data
DATA:
l_rec_layout TYPE slis_layout_alv, "ALV layout setting
l_rec_keyinfo TYPE slis_keyinfo_alv, "Key information
l_h_tbl_sort TYPE slis_sortinfo_alv, "Sort key
l_tbl_sort TYPE slis_t_sortinfo_alv,"Sort table
l_h_tbl_fieldcat TYPE slis_fieldcat_alv,"Field catalog work area
l_tbl_fieldcat TYPE slis_t_fieldcat_alv." Field catalog
* Set layout
l_rec_layout-group_change_edit = 'X'.
l_rec_layout-colwidth_optimize = 'X'.
l_rec_layout-zebra = 'X'.
l_rec_layout-detail_popup = 'X'.
l_rec_layout-get_selinfos = 'X'.
IF p_exp = 'X'.
l_rec_layout-expand_fieldname = 'EXPAND'.
ENDIF.
* Set sort key
l_h_tbl_sort-tabname = 'TBL_VBAK'.
l_h_tbl_sort-fieldname = 'VBELN'.
l_h_tbl_sort-up = 'X'.
APPEND l_h_tbl_sort TO l_tbl_sort.
l_h_tbl_sort-tabname = 'TBL_VBAP'.
l_h_tbl_sort-fieldname = 'VBELN'.
l_h_tbl_sort-up = 'X'.
APPEND l_h_tbl_sort TO l_tbl_sort.
l_h_tbl_sort-tabname = 'TBL_VBAP'.
l_h_tbl_sort-fieldname = 'POSNR'.
l_h_tbl_sort-up = 'X'.
APPEND l_h_tbl_sort TO l_tbl_sort.
* Set Catalog
*--Head
l_h_tbl_fieldcat-tabname = cns_vbak.
l_h_tbl_fieldcat-fieldname = 'VBELN'.
l_h_tbl_fieldcat-ref_tabname = 'VBAK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbak.
l_h_tbl_fieldcat-fieldname = 'KUNNR'.
l_h_tbl_fieldcat-ref_tabname = 'VBAK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbak.
l_h_tbl_fieldcat-fieldname = 'NETWR'.
l_h_tbl_fieldcat-ref_tabname = 'VBAK'.
l_h_tbl_fieldcat-cfieldname = 'WAERK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
CLEAR l_h_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbak.
l_h_tbl_fieldcat-fieldname = 'WAERK'.
l_h_tbl_fieldcat-ref_tabname = 'VBAK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbak.
l_h_tbl_fieldcat-fieldname = 'ERDAT'.
l_h_tbl_fieldcat-ref_tabname = 'VBAK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
*--Items
IF p_chk = 'X'.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'CHECK'.
l_h_tbl_fieldcat-reptext_ddic = '*'.
l_h_tbl_fieldcat-checkbox = 'X'.
l_h_tbl_fieldcat-input = 'X'.
l_h_tbl_fieldcat-edit = 'X'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
CLEAR l_h_tbl_fieldcat.
ENDIF.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'POSNR'.
l_h_tbl_fieldcat-ref_tabname = 'VBAP'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'MATNR'.
l_h_tbl_fieldcat-ref_tabname = 'VBAP'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'ARKTX'.
l_h_tbl_fieldcat-ref_tabname = 'VBAP'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'NETWR'.
l_h_tbl_fieldcat-ref_tabname = 'VBAP'.
l_h_tbl_fieldcat-cfieldname = 'WAERK'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
CLEAR l_h_tbl_fieldcat.
l_h_tbl_fieldcat-tabname = cns_vbap.
l_h_tbl_fieldcat-fieldname = 'WAERK'.
l_h_tbl_fieldcat-ref_tabname = 'VBAP'.
APPEND l_h_tbl_fieldcat TO l_tbl_fieldcat.
* Set keyinfo
l_rec_keyinfo-header01 = 'VBELN'.
l_rec_keyinfo-item01 = 'VBELN'.
l_rec_keyinfo-item02 = 'POSNR'.
* Dipslay Hierarchical list
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'
is_layout = l_rec_layout
it_fieldcat = l_tbl_fieldcat
it_sort = l_tbl_sort
i_tabname_header = cns_vbak
i_tabname_item = cns_vbap
is_keyinfo = l_rec_keyinfo
i_save = 'A'
TABLES
t_outtab_header = tbl_vbak
t_outtab_item = tbl_vbap
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
*&---------------------------------------------------------------------*
*& Form handle_usr_comm
*&---------------------------------------------------------------------*
* Handle user command
*----------------------------------------------------------------------*
FORM user_command USING l_ucomm TYPE sy-ucomm
l_rec_selfield TYPE slis_selfield.
DATA l_h_tbl_vbak TYPE typ_vbak.
CASE l_ucomm.
WHEN '&IC1'. " Pick
CASE l_rec_selfield-tabname.
WHEN cns_vbap.
WHEN cns_vbak.
READ TABLE tbl_vbak
INDEX l_rec_selfield-tabindex
INTO l_h_tbl_vbak.
IF sy-subrc = 0.
* Sales order number
SET PARAMETER ID 'AUN' FIELD l_h_tbl_vbak-vbeln.
* Display Sales Order
CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
ENDIF.
ENDCASE.
ENDCASE.
ENDFORM. " handle_usr_comm