*&---------------------------------------------------------------------*
*& Function: REUSE_ALV_FIELDCATALOG_MERGE *& ALV 求和
*& 和 function: REUSE_ALV_GRID_DISPLAY 一起使用 *& 经典例子
*& 做为一个方法使用,设置ALV的Fieldcatlog.
*& 和delphi 里的dxdbgrid 的功能一样。对小类相加计算,
*& 对所有的数量求和.
*& 对ALV中的某一列进行分类的完成测试程序。
*&---------------------------------------------------------------------*
DATA:BEGIN OF it1 OCCURS 5,
matnr LIKE mara-matnr,
psmng LIKE afpo-psmng,
END OF it1 .
TYPE-POOLS: slis.
DATA: l_events TYPE slis_t_event,
gt_list_top_of_page TYPE slis_t_listheader,
gs_layout TYPE slis_layout_alv,
fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
eventcat TYPE slis_t_event WITH HEADER LINE,
it_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE,
prg LIKE sy-repid.
DATA: l_variant TYPE TABLE OF disvariant WITH HEADER LINE,
START-OF-SELECTION.
CLEAR it1.
it1-matnr = 'AA'.
it1-psmng = 10.
APPEND it1.
CLEAR it1.
it1-matnr = 'AA'.
it1-psmng = 20.
APPEND it1.
CLEAR it1.
it1-matnr = 'BB'.
it1-psmng = 30.
APPEND it1.
CLEAR it1.
it1-matnr = 'BB'.
it1-psmng = 40.
APPEND it1.
CLEAR it1.
it1-matnr = 'BB'.
it1-psmng = 50.
APPEND it1.
CLEAR it1.
it1-matnr = 'CC'.
it1-psmng = 80.
APPEND it1.
END-OF-SELECTION.
PERFORM prepare_list_val.
PERFORM display_val.
*&---------------------------------------------------------------------*
*& Form PREPARE_LIST_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM prepare_list_val.
prg = sy-repid.
l_variant-report = sy-repid.
PERFORM eventtab_build CHANGING l_events.
PERFORM comment_build CHANGING gt_list_top_of_page.
PERFORM set_layout.
ENDFORM. " PREPARE_LIST_VAL
*&---------------------------------------------------------------------*
*& Form EVENTTAB_BUILD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM eventtab_build CHANGING p_events TYPE slis_t_event.
ENDFORM. " EVENTTAB_BUILD
*---------------------------------------------------------------------*
* FORM COMMENT_BUILD *
*---------------------------------------------------------------------*
* --> GT_TOP_OF_PAGE *
*---------------------------------------------------------------------*
FORM comment_build CHANGING gt_top_of_page TYPE slis_t_listheader.
ENDFORM. " COMMENT_BUILD
*&---------------------------------------------------------------------*
*& Form SET_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_layout.
gs_layout-zebra = 'X'.
gs_layout-f2code = '&ETA'.
gs_layout-detail_popup = 'X'.
gs_layout-get_selinfos = 'X'.
ENDFORM. " SET_LAYOUT
*&---------------------------------------------------------------------*
*& Form DISPLAY_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_val.
PERFORM get_fieldcat.
PERFORM get_eventcat.
PERFORM get_it_sort.
PERFORM start_list_val.
ENDFORM. " DISPLAY_VAL
*&---------------------------------------------------------------------*
*& Form GET_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_fieldcat.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = prg
i_internal_tabname = 'IT1'
i_inclname = prg
CHANGING
ct_fieldcat = fieldcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
WRITE: 'SY-SUBRC: ', sy-subrc, 'REUSE_ALV_FIELDCATALOG_MERGE'.
ENDIF.
LOOP AT fieldcat.
CASE fieldcat-fieldname.
WHEN 'MATNR'.
fieldcat-tabname = 'IT1'.
fieldcat-key = 'X'. " Key Column
fieldcat-col_pos = 1. " Position on screen
fieldcat-hotspot = ''. " Hotspot column
fieldcat-seltext_s = 'Name'.
fieldcat-ddictxt = 'S'.
fieldcat-outputlen = 18.
WHEN 'PSMNG'.
fieldcat-tabname = 'IT1'.
fieldcat-key = ' '. " Key Column
fieldcat-col_pos = 2. " Position on screen
fieldcat-hotspot = ' '. " Hotspot column
fieldcat-seltext_s = 'Money'.
fieldcat-ddictxt = 'S'.
fieldcat-outputlen = 15.
fieldcat-no_zero = 'X'.
fieldcat-do_sum = 'X'. "SUM UPON DISPLAY
ENDCASE.
MODIFY fieldcat.
ENDLOOP.
ENDFORM. " GET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form GET_EVENTCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_eventcat.
ENDFORM. " GET_EVENTCAT
*&---------------------------------------------------------------------*
*& Form GET_IT_SORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_it_sort.
CLEAR it_sort.
it_sort-fieldname = 'MATNR'.
it_sort-spos = 1.
it_sort-up = 'X'.
it_sort-subtot = 'X'.
APPEND it_sort.
* CLEAR IT_SORT.
* IT_SORT-FIELDNAME = 'AUFNR'.
* IT_SORT-SPOS = 2.
* IT_SORT-UP = 'X'.
* IT_SORT-SUBTOT = ''.
* APPEND IT_SORT.
ENDFORM. " GET_IT_SORT
*&---------------------------------------------------------------------*
*& Form START_LIST_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM start_list_val.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = prg
is_layout = gs_layout
is_variant = l_variant
it_fieldcat = fieldcat[]
it_sort = it_sort[]
i_save = 'A'
it_events = eventcat[]
TABLES
t_outtab = it1
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " START_LIST_VAL