天天看點

ALV 層級分組顯示報表

ALV 層級分組顯示報表
ALV 層級分組顯示報表

相當于分組顯示。第一行顯示分組的頭,下面顯示明細。

例程1:采購訂單主從表ALV層次輸出

*&---------------------------------------------------------------------*

*& Report Z_T_HIERSEQ_ALV *

*& *

*&---------------------------------------------------------------------*

*& 采購訂單主從表ALV層次輸出 *

*& *

*&---------------------------------------------------------------------*

REPORT z_t_hierseq_alv .

TABLES: ekko,ekpo.

SELECT-OPTIONS s_ebeln FOR ekko-ebeln.

*1、在程式裡包含SLIS

TYPE-POOLS:slis.

*2、聲明主表和明細表

DATA: BEGIN OF headertab OCCURS 0,

ebeln LIKE ekko-ebeln,

bstyp LIKE ekko-bstyp,

bsart LIKE ekko-bsart,

statu LIKE ekko-statu,

END OF headertab.

DATA: BEGIN OF itemtab OCCURS 0,

ebeln LIKE ekpo-ebeln,

ebelp LIKE ekpo-ebelp,

matnr LIKE ekpo-matnr,

werks LIKE ekpo-werks,

menge LIKE ekpo-menge,

netpr LIKE ekpo-netpr,

peinh LIKE ekpo-peinh,

netwr LIKE ekpo-netwr,

END OF itemtab.

DATA: i_fieldcat TYPE slis_t_fieldcat_alv.

DATA: v_repid LIKE sy-repid."目前程式名

*3、申明主表和明細表的名稱變量

DATA: g_tabname_header TYPE slis_tabname,

g_tabname_item TYPE slis_tabname.

*4、聲明主表和明細表連接配接關鍵字變量

DATA: gs_keyinfo TYPE slis_keyinfo_alv.

INITIALIZATION.

v_repid = sy-repid.

START-OF-SELECTION.

PERFORM get_data.

END-OF-SELECTION.

*5、擷取頭表輸出表頭字段名稱

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_program_name = v_repid "目前程式名

i_internal_tabname = 'HEADERTAB' "主表名

i_inclname = v_repid "目前程式名

i_bypassing_buffer = 'X'

i_buffer_active = ''

CHANGING

ct_fieldcat = i_fieldcat "ALV輸出的主表标題

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1

sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*6、擷取從表輸出表頭字段名稱

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_program_name = sy-repid

i_internal_tabname = 'ITEMTAB'

i_inclname = v_repid

i_bypassing_buffer = 'X'

i_buffer_active = ''

CHANGING

ct_fieldcat = i_fieldcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1

sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*7、設定主從表的關聯字段,最多可以設定五個字段關聯。

gs_keyinfo-header01 = 'EBELN'.

gs_keyinfo-item01 = 'EBELN'.

*8、設定主表和明細表變量對應的内表名稱

g_tabname_header = 'HEADERTAB'."主表

g_tabname_item = 'ITEMTAB'."明細表

*9、調用層次ALV輸出函數

CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'

EXPORTING

i_callback_program = v_repid

it_fieldcat = i_fieldcat

i_save = 'A'

i_tabname_header = g_tabname_header

i_tabname_item = g_tabname_item

is_keyinfo = gs_keyinfo

i_bypassing_buffer = 'X'

i_buffer_active = ' '

TABLES

t_outtab_header = headertab

t_outtab_item = itemtab

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.

*&---------------------------------------------------------------------*

*& Form get_data

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

FORM get_data.

SELECT ebeln bstyp bsart statu

INTO TABLE headertab

FROM ekko

WHERE ebeln IN s_ebeln.

IF NOT headertab[] IS INITIAL.

SELECT ebeln ebelp matnr werks menge netpr peinh netwr

INTO TABLE itemtab

FROM ekpo

FOR ALL ENTRIES IN headertab

WHERE ebeln = headertab-ebeln.

ENDIF.

ENDFORM. "get_data

還有官方的demo BALVHD01詳細參見http://www.se80.co.uk/sapreports/b/balv/balvhd01.htm

繼續閱讀