在 SAP 业务系统中,大量地使用到了ALV Tree 对象,该对象在表格基础上对同类数据
进行归类,并对各分类能进行数据汇总,如图8-10 所示。
以航班表(SPFLI)为例:
(1)按国家、航线、航班分类;
(2)在右半屏对距离字段进行数量汇总;
(3)在右屏输出起飞国家、起飞城市、目的国家、目的城市、距离字段,并控制其输出长
度。
处理过程:
(1)建立程序和屏幕,在屏幕上建立定制控制对象,定义好工作流;
(2)建立好PAI、PBO 事件;
(3)在PAI 中定义建立定制控制对象,并建立AVL Tree 对象;
(4)建立ALV Tree 对象的标题;
(5)建立右屏输出字段清单、字段长度、汇总字段等内容;
(6)建立Tree 分类字段清单及输出先后顺序;
(7)显示ALV Tree 对象。
REPORT ytest32.
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
DATA: gb_fieldcat TYPE lvc_t_fcat.
DATA: gb_sortfld TYPE lvc_t_sort.
DATA wa_spfli TYPE TABLE OF spfli.
SELECT * INTO TABLE wa_spfli FROM spfli.
DATA: wa_container TYPE scrfname VALUE 'ALVDATA',
alv_grid TYPE REF TO cl_gui_alv_tree_simple,
wa_custom_container TYPE REF TO cl_gui_custom_container.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
IF wa_custom_container IS INITIAL.
DATA ls_list_comm TYPE slis_t_listheader.
DATA ls_alist_comm TYPE slis_listheader.
ls_alist_comm-typ = 'H'.
ls_alist_comm-info = 'My ALV TREE Testing'.
APPEND ls_alist_comm TO ls_list_comm.
PERFORM bldcat.
PERFORM bldsortfld.
CREATE OBJECT wa_custom_container
EXPORTING
container_name = wa_container.
CREATE OBJECT alv_grid
EXPORTING
i_parent = wa_custom_container.
CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
it_list_commentary = ls_list_comm
i_structure_name = 'SPFLI'
CHANGING
it_sort = gb_sortfld
it_fieldcatalog = gb_fieldcat
it_outtab = wa_spfli.
CALL METHOD alv_grid->expand_tree
EXPORTING
i_level = 1.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form BLDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM bldcat .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SPFLI'
CHANGING
ct_fieldcat = gb_fieldcat.
DATA ls_fldcat TYPE lvc_s_fcat.
LOOP AT gb_fieldcat INTO ls_fldcat.
CASE ls_fldcat-fieldname.
WHEN 'COUNTRYFR' OR 'CITYFROM' OR 'COUNTRYTO'
OR 'CITYTO' OR 'DISTANCE'.
ls_fldcat-outputlen = 15.
WHEN OTHERS.
ls_fldcat-no_out = 'X'.
ENDCASE.
IF ls_fldcat-fieldname = 'DISTANCE'.
ls_fldcat-do_sum = 'X'.
ENDIF.
MODIFY gb_fieldcat FROM ls_fldcat.
ENDLOOP.
ENDFORM. " BLDCAT
*&---------------------------------------------------------------------*
*& Form BLDSORTFLD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM bldsortfld .
DATA ls_sortfld TYPE lvc_s_sort.
ls_sortfld-spos = 1.
ls_sortfld-fieldname = 'COUNTRYFR'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
ls_sortfld-spos = 2.
ls_sortfld-fieldname = 'CARRID'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
ls_sortfld-spos = 3.
ls_sortfld-fieldname = 'CONNID'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
ENDFORM. " BLDSORTFLD
*逻辑流程序:
process before output.
MODULE status_0100.
process after input.
MODULE user_command_0100.