ABAP学习(21):OOSALV学习TreeALV
5. OO SALV tree
利⽤函数和GRID的功能发布的SALV包程序(SALV也称为New ALV)。通过类cl_salv_tree,实现tree类型ALV的输出。
5.1显⽰tree ALV
通过cl_salv_tree类对象lo_tree,显⽰tree ALV。
代码实例:
全局变量定义,使⽤icon需要引⼊type-pools:icon;gt_spfli显⽰的内表结构,是⼀个空内表;lo_tree为cl_salv_tree类对象;lo_error为异常类对象;lo_container为容器类对象。
"使⽤icon
type-pools: icon.
"这个内表创建alv时,没有数据
DATA:gt_spfli TYPE STANDARD TABLE OF spfli.
"cl_salv_tree类对象
五味子片DATA:lo_tree TYPE REF TO cl_salv_tree.
"异常类对象
DATA:lo_error TYPE REF TO CX_SALV_ERROR.
"屏幕容器类对象
DATA:lo_container TYPE REF TO cl_gui_custom_container.
创建cl_salv_tree类对象,设置显⽰tree结构
"创建tree alv,全屏显⽰
FORM creat_alv_fullscreen.
TRY .
"获取cl_salv_tree类对象
cl_salv_tree=>factory(
importing
r_salv_tree = lo_tree
changing
t_table = gt_spfli ).零售企业
"设置全局
PERFORM t_tree_ttings.
"获取数据,设置tree结构
PERFORM t_tree.
"显⽰alv
lo_tree->display( ).
CATCH CX_SALV_NO_NEW_DATA_ALLOWED CX_SALV_ERROR INTO lo_error.
DATA:result TYPE string.
result = lo_error->IF_MESSAGE~GET_TEXT( ).
MESSAGE result TYPE'E'.
ENDTRY.
ENDFORM.
设置tree的树形结构,数据是在这个阶段获取的,⽽⽣成cl_salv_tree类对象lo_tree时传⼊的table为空。
代码实例:
"设置tree结构
FORM t_tree.
DATA:lt_spfli TYPE STANDARD TABLE OF spfli.
DATA:lw_spfli LIKE LINE OF lt_spfli.
DATA:l_carrid_key type lvc_nkey.
DATA:l_connid_key type lvc_nkey.
DATA:l_last_key type lvc_nkey.
DATA:lo_nodes type ref to cl_salv_nodes.
DATA:lo_node type ref to cl_salv_node.
DATA:lo_item type ref to cl_salv_item.
"查询数据
SELECT * INTO TABLE lt_spfli FROM spfli.
"获取tree的node节点
"插⼊节点
"IF_SALV_C_NODE_RELATION~FIRST_CHILD:第⼀个child
"IF_SALV_C_NODE_RELATION~FIRST_SIBLING:第⼀个brother
"IF_SALV_C_NODE_RELATION~LAST_CHILD:最后child
"IF_SALV_C_NODE_RELATION~LAST_SIBLING:最后brother
"IF_SALV_C_NODE_RELATION~NEXT_SIBLING:next brother
"IF_SALV_C_NODE_RELATION~PREVIOUS_SIBLING:previous brother
"IF_SALV_C_NODE_RELATION~PARENT:parent节点
lo_nodes = lo_tree->get_nodes( ).
LOOP AT lt_spfli INTO lw_spfli.
ON CHANGE OF lw_spfli-carrid.
TRY .
"添加⼦节点
lo_node = lo_nodes->add_node(
related_node = ''
data_row = lw_spfli
relationship = cl_salv_nodes=>IF_SALV_C_NODE_RELATION~LAST_CHILD ).
l_carrid_key = lo_node->get_key( ).
CATCH cx_salv_msg.
ENDTRY.
ENDON.
ON CHANGE OF lw_spfli-connid.
TRY .
"添加⼦节点
lo_node = lo_nodes->add_node(
related_node = l_carrid_key
data_row = lw_spfli
relationship = cl_salv_nodes=>IF_SALV_C_NODE_RELATION~LAST_CHILD ).
"获取节点key
l_connid_key = lo_node->get_key( ).
CATCH cx_salv_msg.
ENDTRY.
未来生活
ENDON.
"添加最后层
TRY .
"添加⼦节点
回民街美食攻略lo_node = lo_nodes->add_node(
related_node = l_connid_key
data_row = lw_spfli
relationship = cl_salv_nodes=>IF_SALV_C_NODE_RELATION~LAST_CHILD ).
"获取节点key
l_last_key = lo_node->get_key( ).
CATCH cx_salv_msg.
ENDTRY.
ENDLOOP.
ENDFORM.
View Code
5.2使⽤⼦屏幕显⽰tree alv
定义全局的lo_container容器类对象。
1.创建⼦屏幕100
2.创建GUI Status
3.创建屏幕容器CONTAINER
代码实例:
调⽤⼦屏幕程序
"创建⼦屏幕container显⽰alv
FORM creat_alv_screen.
"调⽤⼦屏幕
CALL SCREEN100.
ENDFORM.
⼦屏幕输⼊输⼊module
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
"设置PF-STATUS
SET PF-STATUS'100'.
IF lo_container IS NOT BOUND.
IF cl_salv_tree=>is_offline( ) EQ abap_fal.
CREATE OBJECT lo_container
EXPORTING
container_name = 'CONTAINER'.
ENDIF.
ENDIF.
"创建cl_salv_tree类对象
TRY .
cl_salv_tree=>factory(
EXPORTING
r_container = lo_container
IMPORTING
r_salv_tree = lo_tree
CHANGING
t_table = gt_spfli ).
"设置全局
PERFORM t_tree_ttings.
"获取数据,设置tree结构
PERFORM t_tree.
"显⽰alv
lo_tree->display( ).
CATCH CX_SALV_NO_NEW_DATA_ALLOWED CX_SALV_ERROR. EXIT.
ENDTRY.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text ur command
*----------------------------------------------------------------------*
企业文化的理解
MODULE USER_COMMAND_0100 INPUT.
DATA:ok_code TYPE syucomm.
ok_code = sy-ucomm.
CASE ok_code.
WHEN 'BACK'.
CLEAR ok_code.
SET SCREEN0.
LEAVE SCREEN.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
View Code
5.3设置tree显⽰
通过cl_salv_tree_ttings类对象lo_tree_ttings设置层级的显⽰参数。
通过cl_salv_tree类对象lo_tree的get_tree_ttings⽅法获取cl_salv_tree_ttings类对象。
代码实例:
"设置tree显⽰
FORM t_tree_ttings.
DATA:lo_tree_ttings TYPE REF TO cl_salv_tree_ttings.
DATA:lo_tooltips TYPE REF TO cl_salv_tooltips.
"获取设置实例化对象
lo_tree_ttings = lo_tree->get_tree_ttings( ).
"设置header
lo_tree_ttings->t_hierarchy_header( 'tree alv' ).
钦佩是什么意思
"设置tooltip
lo_tree_ttings->t_hierarchy_tooltip( 'tree alv tooltip' ).
"设置层级列icon,type pools:icon
lo_tree_ttings->t_hierarchy_icon( '@06@' ).
"设置size,层级列宽度
lo_tree_ttings->t_hierarchy_size( 30 ).
"设置header
DATA:lv_title TYPE salv_de_tree_text.
lv_title = sy-title."系统title
lo_tree_ttings->t_header( lv_title ).
"设置tooltips
lo_tooltips = lo_tree_ttings->get_tooltips( ).
DATA:lv_value TYPE LVC_VALUE.
lv_value = IF_SALV_C_TREE_STYLE=>EMPHASIZED_A.
"C_TYPE_ICON
"C_TYPE_SYMBOL
"C_TYPE_COLOR
"C_TYPE_EXCEPTION
行测技巧"C_TYPE_TREE_STYLE
lo_tooltips->add_tooltip(
EXPORTING
TYPE = CL_SALV_TOOLTIP=>C_TYPE_TREE_STYLE
VALUE = lv_value
TOOLTIP = 'tree style tooltip'
).
ENDFORM.
View Code
5.4设置function
通过cl_salv_tree类对象lo_tree的t_screen_status⽅法设置pf-status。
找到Function Group:KKBL,复制标准的PF-STATUS到Program。但是在tree ALV 中显⽰出来的标准功能,基本上没有⽤,所以需要定制⾃⼰的Tool⼯具栏。
说明怎么写代码实例:
"设置function
FORM t_functions.
"设置⾃定义PF-STATUS
"C_FUNCTIONS_NONE
"C_FUNCTIONS_ALL
"C_FUNCTIONS_DEFAULT
lo_tree->t_screen_status(
pfstatus = 'STANDARD_ALV_TREE'
report = sy-repid
t_functions = lo_tree->c_functions_all ).
ENDFORM.
⾃定义function按钮,使⽤cl_salv_functions和cl_salv_functions_tree类对象。代码实例:
"设置function
FORM t_functions.
DATA:lo_functions_ba TYPE REF TO cl_salv_functions.
DATA:lo_functions_tree TYPE REF TO cl_salv_functions_tree.
"设置⾃定义PF-STATUS
"C_FUNCTIONS_NONE
"C_FUNCTIONS_ALL
"C_FUNCTIONS_DEFAULT
lo_tree->t_screen_status(
pfstatus = 'STANDARD_ALV_TREE'
report = sy-repid
t_functions = lo_tree->c_functions_all ).
"获取当前pf-status
"lo_tree->get_screen_status( IMPORTING report = pfstatus = ).
"使⽤cl_salv_functions,cl_salv_functions_tree添加⾃定义function,
"只能在定义容器显⽰tree ALV时才能⽣效
TRY .
"获取基础function类对象
lo_functions_ba = lo_tree->get_functions_ba( ).
"添加function,好像并没添加
lo_functions_ba->add_function(
name = 'MYFUN1'
icon = 'ICON_SELECT_DETAIL'
text = 'MYFUN1'
tooltip = 'my function'
position = IF_SALV_C_FUNCTION_POSITION=>RIGHT_OF_SALV_FUNCTIONS ). CATCH CX_SALV_EXISTING CX_SALV_WRONG_CALL.
ENDTRY.
TRY.
"设置tree特有function
lo_functions_tree = lo_tree->get_functions( ).
lo_functions_tree->add_function(
name = 'MYFUN2'
icon = 'ICON_SELECT_DETAIL'
text = 'MYFUN2'
tooltip = 'my function2'