*&---------------------------------------------------------------------*
*& Report YTEST00A
*&---------------------------------------------------------------------*
*&Violin 2021/3/10
*&---------------------------------------------------------------------*
REPORT ytest00a.
*第8章 表控件ALV控件的使用---------------------------------------------------------------------*
*8.6ALV Tree的使用
**功能碼返回值
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 580.
MODULE status_0100 OUTPUT.
SET PF-STATUS 'TESTSTA1'.
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.
MODULE user_command_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& 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.
*&---------------------------------------------------------------------*
*& Form BLDSORTFLD
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM bldsortfld .
DATA ls_sortfld TYPE lvc_s_sort.
CLEAR ls_sortfld .
ls_sortfld-spos = 1.
ls_sortfld-fieldname = 'COUNTRYFR'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
CLEAR ls_sortfld .
ls_sortfld-spos = 2.
ls_sortfld-fieldname = 'CARRID'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
CLEAR ls_sortfld .
ls_sortfld-spos = 3.
ls_sortfld-fieldname = 'CONNID'.
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
ENDFORM.
![](https://i-blog.csdnimg.cn/blog_migrate/c9e0553994acd15e0b2d43e021ae4cb6.png)