ALV Tree 学习总结

3 篇文章 0 订阅
3 篇文章 0 订阅

1、系统中有很多标准的例子:T_cood: DWDM有个中控件的例子。另外Package:SLIS里面有ALV方面的所有例子,参考这些例子就能写出很好的程序。
以下是我做的一个程序:功能是多个:sale order的document flow。
基本思路:
1、查询结果中只显示Order type and number, 点击node才触发获取document flow的功能。这样对于提高性能非常重要。
2、call function 'SD_DOCUMENT_FLOW_GET' 可以获得某个order的相关文件。


源代码:
注意要在Screen中添加一个user control :'CSALES'
report zysdf_04_04.
*$*$--------------------------------------------------------------------
*$*$ Object Name: ZYSDF_04_04
*$*$ Author: CH210276
*$*$ Date: 20081229
*$*$ Copyright:
*$*$ Transport Number:
*$*$
*$*$ Logical Database:
*$*$ SAPScript name:
*$*$ Application Area: SD
*$*$ Description: Sales document flow
*$*$
*$*$ Purpose: display some sales document flow
*$*$--------------------------------------------------------------------
*$*$ Correction #
*$*$ Modified By
*$*$ Date
*$*$ Transport Number
*$*$--------------------------------------------------------------------
include zysdf_04_04_data.
include zysdf_04_04_pbo.
include zysdf_04_04_pai.
include zysdf_04_04_form.
"INCLUDE zysdf_04_04_class.

*$*$--------------------------------------------------------------------
*$*$ TOP OF PAGE Event
*$*$--------------------------------------------------------------------
top-of-page.
* Subroutine for Top of Page event.
* perform top_of_page.

*$*$--------------------------------------------------------------------
*$*$ END OF PAGE Event
*$*$--------------------------------------------------------------------
end-of-page.
* Subroutine for End of Page event.
* perform end_of_page.

*$*$--------------------------------------------------------------------
*$*$ START OF SELECTION Event
*$*$--------------------------------------------------------------------
start-of-selection.


call screen 101.
* Populate the final output table for display

*$*$--------------------------------------------------------------------
*$*$ END OF SELECTION Event
*$*$--------------------------------------------------------------------
end-of-selection.
*&---------------------------------------------------------------------*
*& Include ZYSDF_04_04_DATA
*&---------------------------------------------------------------------*

*-------------------------------------------------------------------
* Essential steps (Search for '§')
* ~~~~~~~~~~~~~~~
* 1.Usual steps when using control technology.
* 1a. Define reference variables.
* 1b. Create ALV Tree Control and corresponding container.
*
* 2.Create Hierarchy-header
* 3.Create empty Tree Control
* 4.Create hierarchy (nodes and leaves)
* 4a. Select data
* 4b. Sort output table according to your conceived hierarchy
* 4c. Add data to tree
*
* 5.Send data to frontend.
* 6.Call dispatch to process toolbar functions
*
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
class cl_gui_cfw definition load.

data: g_custom_container type ref to cl_gui_custom_container," custom_container
gt_fieldcatalog type lvc_t_fcat, " alv tree header field
g_tree type ref to cl_gui_alv_tree," object of alv tree
ok_code type sy-ucomm. " for screen 101

* Structure for select from table VBAK
types: begin of ty_vbak,
vbeln type vbak-vbeln, "Sales Document
vbtyp type vbak-vbtyp, "SD document category
ddtext type dd07t-ddtext , "Short Text for Fixed Values
end of ty_vbak.
* internal table
data i_vbak type standard table of ty_vbak initial size 0.
* work area
data wa_vbak type ty_vbak.
* internal table
data i_outdata type standard table of zvbfa initial size 0.
* work area
data wa_outdata type zvbfa.
*$*$ Variables
* Variable declaration for Select-Option: S_vbeln
data g_vbeln type vbak-vbeln.
* Variable declaration for Select-Option: S_VKORG
data g_vkorg type vbak-vkorg.
* Variable declaration for Select-Option: S_VTWEG
data g_vtweg type vbak-vtweg.
* Variable declaration for Select-Option: S_SPART
data g_spart type vbak-spart.
* Variable declaration for Select-Option: S_VKGRP
data g_vkgrp type vbak-vkgrp.
* Variable declaration for Select-Option: S_VKBUR
data g_vkbur type vbak-vkbur.
*$*$--------------------------------------------------------------------
*$*$ Selection criteria
*$*$--------------------------------------------------------------------
selection-screen begin of block a with frame title text-001.
select-options s_vbeln for g_vbeln. "for Select-Option: order
select-options s_vkorg for g_vkorg. "for Select-Option: orgnization
select-options s_vtweg for g_vtweg. "for Select-Option: Distribution Channel
select-options s_spart for g_spart. "for Select-Option: Division
select-options s_vkgrp for g_vkgrp. "for Select-Option: Sales Group
select-options s_vkbur for g_vkbur. "for Select-Option: Sales Office
selection-screen end of block a.
*$*$--------------------------------------------------------------------
*$*$ INITIALIZATION Event
*$*$--------------------------------------------------------------------
initialization.
*$*$--------------------------------------------------------------------
*$*$ AT SELECTION SCREEN ON Event
*$*$--------------------------------------------------------------------
*$*$ Validate Sales Order
at selection-screen on s_vbeln.
perform sub_validate_vbeln.
*$*$ Validate Sales Organization
at selection-screen on s_vkorg.
perform sub_validate_vkorg.
*$*$ Validate Distribution Channel
at selection-screen on s_vtweg.
perform sub_validate_vtweg.
*$*$ Validate Division
at selection-screen on s_spart.
perform sub_validate_spart.
*$*$ Validate Sales Group
at selection-screen on s_vkgrp.
perform sub_validate_vkgrp.
*$*$ Validate Sales Office
at selection-screen on s_vkbur.
perform sub_validate_vkbur.

*&---------------------------------------------------------------------*
*& Include ZYSDF_04_04_PBO
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_0101 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0101 output.
set pf-status 'STATUS01'.
* SET TITLEBAR 'xxx'.
if g_tree is initial.
perform sub_init_tree.
endif.
call method cl_gui_cfw=>flush.
endmodule. " STATUS_0101 OUTPUT

*&---------------------------------------------------------------------*
*& Include ZYSDF_04_04_PAI
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0101 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0101 input.
case ok_code.
when 'EXIT' or 'BACK' or 'CANC'.
perform sub_exit.

when others.
call method cl_gui_cfw=>dispatch.
endcase.
clear ok_code.
call method cl_gui_cfw=>flush.
endmodule. " USER_COMMAND_0101 INPUT

*&---------------------------------------------------------------------*
*& Include ZYSDF_04_04_FORM
*&---------------------------------------------------------------------*

class lcl_tree_event_receiver definition.
public section.
methods handle_expand_no_children
for event expand_nc
of cl_gui_alv_tree
importing node_key sender.
endclass. "lcl_tree_event_receiver DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_tree_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_tree_event_receiver implementation.
**********************************************************************
* METHOD : handle_expand_no_children
* Created : 30.12.2008 16:33:33
*
* when user click the header node,this method will be perform.
**********************************************************************
method handle_expand_no_children.
types: begin of lty_node,
node type i, "Sales Document
hvbeln type vbak-vbeln , "SD document category
end of lty_node.
data lw_node type lty_node.
data li_node type standard table of lty_node initial size 0.
data l_docflow type tdt_docflow.
data lw_docflow type tds_docflow.
data: lt_children type lvc_t_nkey.
data: lw_outdata type zvbfa.
data: "l_docnum type vbak-vbeln,
l_docnuv type vbak-vbeln,
" l_docnum1 type vbak-vbeln,
l_docnuv1 type vbak-vbeln,
l_key0 type lvc_nkey,
l_key1 type lvc_nkey,
l_key2 type lvc_nkey,
l_node_text type lvc_value,
l_level type i.
* get data of the selected item
read table i_outdata into wa_outdata index node_key.
* get the order ducoment flow
call function 'SD_DOCUMENT_FLOW_GET'
exporting
iv_docnum = wa_outdata-vbelv
* IV_ITEMNUM =
" IV_ALL_ITEMS = 'X'
importing
et_docflow = l_docflow
.
l_key0 = node_key .
l_key1 = node_key.
l_level = 1.
* add child's item

loop at l_docflow into lw_docflow .


concatenate lw_docflow-description lw_docflow-vbeln
into l_node_text.
lw_outdata-vbelv = lw_docflow-vbelv.
lw_outdata-posnv = lw_docflow-posnv.
lw_outdata-vbeln = lw_docflow-vbeln.
lw_outdata-posnn = lw_docflow-posnn.
lw_outdata-matnr = lw_docflow-matnr.
lw_outdata-rfmng = lw_docflow-rfmng.
lw_outdata-meins = lw_docflow-meins.
lw_outdata-rfwrt = lw_docflow-rfwrt.
lw_outdata-waers = lw_docflow-erdat.
lw_outdata-erdat = lw_docflow-erdat.

clear lw_node.
loop at li_node into lw_node where hvbeln eq lw_docflow-docnuv .

endloop.
if lw_node is initial.
lw_node-node = l_key1.
l_key0 = l_key1.
lw_node-hvbeln = lw_docflow-docnuv.
append lw_node to li_node.
else.
l_key0 = lw_node-node.
endif.

call method g_tree->add_node
exporting
i_relat_node_key = l_key0
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_outtab_line = lw_outdata "is_node_layout = ls_node_layout
importing
e_new_node_key = l_key1.
endloop.
* expend the childrens
describe table li_node lines l_level.
call method sender->get_children
exporting
i_node_key = node_key
importing
et_children = lt_children.

if not lt_children is initial.

call method sender->expand_node
exporting
i_node_key = node_key
i_level_count = l_level.
endif.
endmethod. "handle_expand_no_children
endclass. "lcl_tree_event_receiver IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Form sub_init_tree
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form sub_init_tree .
" the container is linked to the custom control with the name 'CSALE' on the dynpro
create object g_custom_container
exporting
container_name = 'CSALES'
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
* create the alv tree
create object g_tree
exporting
parent = g_custom_container " the tree's container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_multiple
item_selection = ''
no_html_header = ''
no_toolbar = ''
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
illegal_node_selection_mode = 5
failed = 6
illegal_column_name = 7.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
* set the tree's header
data l_hierarchy_header type treev_hhdr.
perform build_hierarchy_header changing l_hierarchy_header.
* set the logo
data: l_logo type sdydo_value,
lt_list_commentary type slis_t_listheader.
perform build_comment using
lt_list_commentary
l_logo.

perform build_fieldcatalog.
* IMPORTANT: Table 'i_outdata' must be empty. Do not change this table
* (even after this method call). You can change data of your table
* by calling methods of CL_GUI_ALV_TREE.
* Furthermore, the output table 'gt_outtab' must be global and can
* only be used for one ALV Tree Control.
call method g_tree->set_table_for_first_display
exporting
is_hierarchy_header = l_hierarchy_header
it_list_commentary = lt_list_commentary
i_logo = l_logo
i_background_id = 'ALV_BACKGROUND'
changing
it_fieldcatalog = gt_fieldcatalog
it_outtab = i_outdata. "table must be empty !

perform create_hierarchy.
perform register_events.
* display the tree
call method g_tree->frontend_update.

endform. "sub_init_tree
**********************************************************************
* FORM : create_hierarchy
* Created : 30.12.2008 18:30:32
**********************************************************************
form create_hierarchy .
data: lw_outdata type zvbfa.
data: p_node_key type lvc_nkey.
data: l_node_text type lvc_value.

data: ls_node_layout type lvc_s_layn.


ls_node_layout-isfolder = 'X'.
ls_node_layout-expander = 'X'.

perform sub_select_data_vbak.
check not i_vbak is initial.
sort i_vbak by vbeln.
loop at i_vbak into wa_vbak .
lw_outdata-vbelv = wa_vbak-vbeln.
concatenate wa_vbak-ddtext ':' wa_vbak-vbeln into l_node_text.

call method g_tree->add_node
exporting
i_relat_node_key = space
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_node_layout = ls_node_layout
is_outtab_line = lw_outdata
importing
e_new_node_key = p_node_key.
endloop.
endform. "create_hierarchy
**********************************************************************
* FORM : register_events
* Created : 31.12.2008 13:54:31
**********************************************************************
form register_events .
data: lt_events type cntl_simple_events,
l_event type cntl_simple_event,
l_event_receiver type ref to lcl_tree_event_receiver.

call method g_tree->get_registered_events
importing
events = lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_expand_no_children.
append l_event to lt_events.

call method g_tree->set_registered_events
exporting
events = lt_events
exceptions
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.
if sy-subrc <> 0.
message x208(00) with 'ERROR'. "#EC NOTEXT
endif.
*--------------------
*§4d. Register events on backend (ABAP Objects event handling)
create object l_event_receiver.
set handler l_event_receiver->handle_expand_no_children for g_tree.
endform. " register_events

*&---------------------------------------------------------------------*
*& Form build_comment
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_LIST_COMMENTARY text
* -->P_LOGO text
*----------------------------------------------------------------------*
form build_comment using
pt_list_commentary type slis_t_listheader
p_logo type sdydo_value.

data: ls_line type slis_listheader.
*
* LIST HEADING LINE: TYPE H
clear ls_line.
ls_line-typ = 'H'.
* LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-info = 'SD: Document Flow Chart'. "Header information
append ls_line to pt_list_commentary.
* STATUS LINE: TYPE S
clear ls_line.
ls_line-typ = 'S'.
ls_line-key = 'User'. "User
ls_line-info = sy-uname. "User name
append ls_line to pt_list_commentary.
ls_line-key = 'System'.
ls_line-info = sy-sysid. "#EC NOTEXT
append ls_line to pt_list_commentary.
* ACTION LINE: TYPE A
clear ls_line.
ls_line-typ = 'S'.
* LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-key = 'Date'.
ls_line-info = sy-datum.
append ls_line to pt_list_commentary.
* Current time
clear ls_line.
ls_line-typ = 'S'.
ls_line-key = 'Time'.
ls_line-info = sy-uzeit.
append ls_line to pt_list_commentary.

p_logo = 'ENJOYSAP_LOGO'.

endform. "build_comment
*&---------------------------------------------------------------------*
*& Form build_hierarchy_header
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_HIERARCHY_HEADER text
*----------------------------------------------------------------------*
form build_hierarchy_header changing
p_hierarchy_header type treev_hhdr.

p_hierarchy_header-heading = 'Sales DOC type/number'.
p_hierarchy_header-tooltip = 'Sales document'.
p_hierarchy_header-width = 55.
p_hierarchy_header-width_pix = ''.

endform. "build_hierarchy_header
*&---------------------------------------------------------------------*
*& Form build_fieldcatalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form build_fieldcatalog.
data: ls_fieldcatalog type lvc_s_fcat.

* The following function module generates a fieldcatalog according
* to a given structure.
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'ZVBFA'
changing
ct_fieldcat = gt_fieldcatalog.

* The fieldcatalog is provided in form 'init_tree' using method
* set_table_for_first_display.
endform. "build_fieldcatalog
*&---------------------------------------------------------------------*
*& Form SUB_EXIT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form sub_exit .
if not g_tree is initial.
call method g_tree->free.
endif.

leave to screen 0.
endform. " SUB_EXIT
*&---------------------------------------------------------------------*
*& Form sub_select_data_vbak
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form sub_select_data_vbak.

* Select data from table
select vbak~vbeln
vbak~vbtyp
dd07t~ddtext
from vbak
inner join dd07t
on dd07t~domvalue_l = vbak~vbtyp
and dd07t~domname = 'VBTYP'
and dd07t~ddlanguage = 'E'
into table i_vbak
where vbeln in s_vbeln "Sales Document
and vkorg in s_vkorg "Sales Org.
and vtweg in s_vtweg "Distr. Channel
and spart in s_spart "Division
and vkgrp in s_vkgrp "Sales Group
and vkbur in s_vkbur. "Sales Office

* Issue error message if the select fails
if sy-subrc ne 0.
message i398(00) with 'No data selected from table'(e03)
'VBAK'(008) space space.
leave list-processing.
endif.

endform. " FORM SUB_SELECT_DATA_VBAK

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_vbeln
*$*$--------------------------------------------------------------------
form sub_validate_vbeln.

data l_vbeln type vbak-vbeln.

* Check for initial value before validating
check not s_vbeln is initial.

* Select Sales Order for validation
select vbeln up to 1 rows
from vbak into l_vbeln
where vbeln in s_vbeln.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Sales Order'(002) space space.
endif.

endform. " FORM SUB_VALIDATE_VBELN

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_VKORG
*$*$--------------------------------------------------------------------
form sub_validate_vkorg.

data l_vkorg type vbak-vkorg.

* Check for initial value before validating
check not s_vkorg is initial.

* Select Sales Organization for validation
select vkorg up to 1 rows
from vbak into l_vkorg
where vkorg in s_vkorg.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Sales Organization'(003) space space.
endif.

endform. " FORM SUB_VALIDATE_VKORG

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_VTWEG
*$*$--------------------------------------------------------------------
form sub_validate_vtweg.

data l_vtweg type vbak-vtweg.

* Check for initial value before validating
check not s_vtweg is initial.

* Select Distribution Channel for validation
select vtweg up to 1 rows
from vbak into l_vtweg
where vtweg in s_vtweg.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Distribution Channel'(004) space space.
endif.

endform. " FORM SUB_VALIDATE_VTWEG

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_SPART
*$*$--------------------------------------------------------------------
form sub_validate_spart.

data l_spart type vbak-spart.

* Check for initial value before validating
check not s_spart is initial.

* Select Division for validation
select spart up to 1 rows
from vbak into l_spart
where spart in s_spart.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Division'(005) space space.
endif.

endform. " FORM SUB_VALIDATE_SPART

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_VKGRP
*$*$--------------------------------------------------------------------
form sub_validate_vkgrp.

data l_vkgrp type vbak-vkgrp.

* Check for initial value before validating
check not s_vkgrp is initial.

* Select Sales Group for validation
select vkgrp up to 1 rows
from vbak into l_vkgrp
where vkgrp in s_vkgrp.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Sales Group'(006) space space.
endif.

endform. " FORM SUB_VALIDATE_VKGRP

*$*$--------------------------------------------------------------------
*$*$ Form SUB_VALIDATE_VKBUR
*$*$--------------------------------------------------------------------
form sub_validate_vkbur.

data l_vkbur type vbak-vkbur.

* Check for initial value before validating
check not s_vkbur is initial.

* Select Sales Office for validation
select vkbur up to 1 rows
from vbak into l_vkbur
where vkbur in s_vkbur.
endselect.

* Stop processing and issue error message in case of error
if sy-subrc ne 0.
message e398(00) with 'Incorrect entry for'(e02)
'Sales Office'(007) space space.
endif.

endform. " FORM SUB_VALIDATE_VKBUR

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值