ABAP学习(21):OOSALV学习 Tree ALV
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 set_tree_settings. "获取数据,设置tree结构 PERFORM set_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 set_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.
5.2使用子屏幕显示tree alv
定义全局的lo_container容器类对象。
1.创建子屏幕100
2.创建GUI Status
3.创建屏幕容器CONTAINER
代码实例:
调用子屏幕程序
"创建子屏幕container显示alv FORM creat_alv_screen. "调用子屏幕 CALL SCREEN 100. 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_false. 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 set_tree_settings. "获取数据,设置tree结构 PERFORM set_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 user command *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. DATA:ok_code TYPE syucomm. ok_code = sy-ucomm. CASE ok_code. WHEN 'BACK'. CLEAR ok_code. SET SCREEN 0. LEAVE SCREEN. ENDCASE. ENDMODULE. " USER_COMMAND_0100 INPUT
5.3设置tree显示
通过cl_salv_tree_settings类对象lo_tree_settings设置层级的显示参数。
通过cl_salv_tree类对象lo_tree的get_tree_settings方法获取cl_salv_tree_settings类对象。
代码实例:
"设置tree显示 FORM set_tree_settings. DATA:lo_tree_settings TYPE REF TO cl_salv_tree_settings. DATA:lo_tooltips TYPE REF TO cl_salv_tooltips. "获取设置实例化对象 lo_tree_settings = lo_tree->get_tree_settings( ). "设置header lo_tree_settings->set_hierarchy_header( 'tree alv' ). "设置tooltip lo_tree_settings->set_hierarchy_tooltip( 'tree alv tooltip' ). "设置层级列icon,type pools:icon lo_tree_settings->set_hierarchy_icon( '@06@' ). "设置size,层级列宽度 lo_tree_settings->set_hierarchy_size( 30 ). "设置header DATA:lv_title TYPE salv_de_tree_text. lv_title = sy-title."系统title lo_tree_settings->set_header( lv_title ). "设置tooltips lo_tooltips = lo_tree_settings->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.
5.4设置function
通过cl_salv_tree类对象lo_tree的set_screen_status方法设置pf-status。
找到Function Group:KKBL,复制标准的PF-STATUS到Program。但是在tree ALV 中显示出来的标准功能,基本上没有用,所以需要定制自己的Tool工具栏。
代码实例:
"设置function FORM set_functions. "设置自定义PF-STATUS "C_FUNCTIONS_NONE "C_FUNCTIONS_ALL "C_FUNCTIONS_DEFAULT lo_tree->set_screen_status( pfstatus = 'STANDARD_ALV_TREE' report = sy-repid set_functions = lo_tree->c_functions_all ). ENDFORM.
自定义function按钮,使用cl_salv_functions和cl_salv_functions_tree类对象。
代码实例:
"设置function FORM set_functions. DATA:lo_functions_base 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->set_screen_status( pfstatus = 'STANDARD_ALV_TREE' report = sy-repid set_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_base = lo_tree->get_functions_base( ). "添加function,好像并没添加 lo_functions_base->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' position = IF_SALV_C_FUNCTION_POSITION=>RIGHT_OF_SALV_FUNCTIONS ). CATCH CX_SALV_EXISTING CX_SALV_WRONG_CALL. ENDTRY. ENDFORM.
5.5设置列
通过cl_salv_tree类对象lo_tree的方法get_columns获取列设置类cl_salv_columns_tree对象lo_columns。
代码实例:
"设置列 FORM set_columns. DATA:lo_columns TYPE REF TO cl_salv_columns_tree. DATA:lt_columns TYPE SALV_T_COLUMN_REF. DATA:lo_column TYPE REF TO cl_salv_column. TRY . "获取列设置对象 lo_columns = lo_tree->get_columns( ). "设置自适应列宽 lo_columns->set_optimize( abap_true ). "获取column内表,所有列 lt_columns = lo_columns->get( ). "获取单列 lo_column = lo_columns->get_column( columnname = 'CARRID' ). "获取列位置 DATA:lv_pos. lv_pos = lo_columns->get_column_position( columnname = 'CARRID' ). "设置列位置 lo_columns->set_column_position( columnname = 'CARRID' position = 3 ). "获取,设置exception列 "SET_EXCEPTION_COLUMN "GET_EXCEPTION_COLUMN "通过cl_salv_column设置单列 lo_column->set_alignment( IF_SALV_C_ALIGNMENT=>LEFT ). lo_column->set_short_text( '航班id' ). "设置隐藏 lo_column = lo_columns->get_column( columnname = 'AIRPFROM' ). lo_column->set_visible( abap_false ). lo_column = lo_columns->get_column( columnname = 'AIRPTO' ). lo_column->set_technical( abap_true ). "设置quantity lo_column = lo_columns->get_column( columnname = 'DISTANCE' ). lo_column->set_quantity( 'C36' ). "SET_ALIGNMENT设置对齐方式 "IF_SALV_C_ALIGNMENT=>LEFT/RIGHT/CENTERED "SET_CURRENCY_COLUMN,设置currency列 "SET_CURRENCY,设置currency "SET_DDIC_REFERENCE:设置数据库参考数据结构 "SET_DECIMALS_COLUMN"设置小数列 "SET_DECIMALS"设置整列小数位数 "SET_EDIT_MASK"设置格式显示 "SET_F1_ROLLNAME "SET_LEADING_ZERO "设置前置0 "SET_LOWERCASE "是否可转换小写 "SET_LONG_TEXT "设置列字段名 "SET_MEDIUM_TEXT"设置列字段名 "SET_SHORT_TEXT"设置列字段名 "SET_OPTIMIZED "设置自适应宽度 "SET_OUTPUT_LENGTH"设置输出长度 "SET_QUANTITY "设置数量 "SET_QUANTITY_COLUMN "设置数量列 "SET_ROUND "设置四舍五入 "SET_ROUND_COLUMN"设置四舍五入列 "SET_ROW "SET_SIGN "设置显示sign "SET_TECHNICAL "设置technical隐藏 "SET_VISIBLE "设置是否可见 "SET_TOOLTIP "SET_ZERO "SET_DECFLOAT_STYLE "GET_DECFLOAT_STYLE "SET_FIXED_HEADER_TEXT CATCH CX_SALV_NOT_FOUND. ENDTRY. ENDFORM.
5.6设置event事件
通过自定义事件处理类lcl_handle_events,然后实现该类;
通过cl_salv_tree类对象lo_tree的get_event方法获取cl_salv_events_tree类对象,然后将事件处理类方法注册给该类对象。
代码实例:
"定义事件处理类 CLASS lcl_handle_events DEFINITION. PUBLIC SECTION. METHODS:on_user_command FOR EVENT added_function OF cl_salv_events IMPORTING e_salv_function. METHODS:on_before_user_command FOR EVENT before_salv_function OF cl_salv_events IMPORTING e_salv_function. METHODS:on_after_user_command FOR EVENT after_salv_function OF cl_salv_events IMPORTING e_salv_function. METHODS:on_double_click FOR EVENT double_click OF cl_salv_events_tree IMPORTING node_key columnname. METHODS:on_link_click FOR EVENT link_click OF cl_salv_events_tree IMPORTING columnname. METHODS:on_keypress FOR EVENT keypress OF cl_salv_events_tree IMPORTING node_key columnname key. METHODS:on_checkbox_change FOR EVENT checkbox_change OF cl_salv_events_tree IMPORTING columnname node_key checked. METHODS:on_expand_empty FOR EVENT expand_empty_folder OF cl_salv_events_tree IMPORTING node_key. ENDCLASS. "事件处理类实现 CLASS lcl_handle_events IMPLEMENTATION. METHOD on_user_command. MESSAGE i000(0k) WITH e_salv_function. ENDMETHOD. METHOD on_before_user_command. MESSAGE i000(0k) WITH 'before:' e_salv_function. ENDMETHOD. METHOD on_after_user_command. MESSAGE i000(0k) WITH 'after:' e_salv_function. ENDMETHOD. METHOD on_double_click. MESSAGE i000(0k) WITH node_key columnname. ENDMETHOD. METHOD on_link_click. MESSAGE i000(0k) WITH columnname. ENDMETHOD. METHOD on_keypress. MESSAGE i000(0k) WITH node_key columnname key. ENDMETHOD. METHOD on_checkbox_change. MESSAGE i000(0k) WITH columnname node_key checked. ENDMETHOD. METHOD on_expand_empty. MESSAGE i000(0k) WITH node_key. ENDMETHOD. ENDCLASS. "设置event FORM set_events. DATA:lo_events TYPE REF TO cl_salv_events_tree. DATA:lo_handle_events TYPE REF TO lcl_handle_events. TRY . "获取event类对象 lo_events = lo_tree->get_event( ). "创建事件处理类对象 CREATE OBJECT lo_handle_events. "注册事件 SET HANDLER lo_handle_events->on_user_command FOR lo_events. SET HANDLER lo_handle_events->on_before_user_command FOR lo_events. SET HANDLER lo_handle_events->on_after_user_command FOR lo_events. SET HANDLER lo_handle_events->on_double_click FOR lo_events. SET HANDLER lo_handle_events->on_link_click FOR lo_events. SET HANDLER lo_handle_events->on_keypress FOR lo_events. SET HANDLER lo_handle_events->on_checkbox_change FOR lo_events. SET HANDLER lo_handle_events->on_expand_empty FOR lo_events. "keypress lo_events->add_key_for_keypress( if_salv_c_keys=>f1 ). lo_events->add_key_for_keypress( if_salv_c_keys=>f4 ). lo_events->add_key_for_keypress( if_salv_c_keys=>enter ). CATCH CX_SALV_MSG. ENDTRY. ENDFORM.
5.7设置Node节点
创建树形结构添加节点时,通过cl_salv_nodes类对象lo_nodes添加新的节点,删除节点,移动节点。
代码实例:
"设置node节点 FORM set_nodes. DATA:lt_spfli TYPE TABLE OF spfli. DATA:lw_spfli LIKE LINE OF lt_spfli. "节点类对象 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. "key DATA:lv_key type lvc_nkey. "设置节点内容 lw_spfli-carrid = 'BB'. lw_spfli-connid = '11'. lw_spfli-countryfr = 'CN'. lw_spfli-cityfrom = 'CQ'. "获取节点类对象 lo_nodes = lo_tree->get_nodes( ). TRY . "添加节点 "related_node:父节点 "relationship:设置添加节点和父节点关系 "cl_salv_nodes "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节点 "或者cl_gui_column_tree "RELAT_FIRST_CHILD "RELAT_FIRST_SIBLING "RELAT_LAST_CHILD "RELAT_LAST_SIBLING "RELAT_NEXT_SIBLING "RELAT_PREV_SIBLING "add_node参数时创建时传入,也可以使用lo_node的方法设置 "data_row:本节点数据,使用SET_DATA_ROW,GET_DATA_ROW "collapsed_icon:收拢icon,使用SET_COLLAPSED_ICON,GET_COLLAPSED_ICON "expanded_icon:展开icon,使用SET_EXPANDED_ICON,GET_EXPANDED_ICON "row_style:行类型,使用SET_ROW_STYLE,GET_ROW_STYLE "text:tree控制列文本,使用SET_TEXT,GET_TEXT "visible:是否可见,使用SET_VISIBLE,IS_VISIBLE, "expander:是否可展开,使用SET_EXPANDER,HAS_EXPANDER "enabled:是否使能,使用SET_ENABLED,IS_ENABLED "folder:是否活页夹,使用SET_FOLDER,IS_FOLDER "COLLAPSE:收拢节点 "EXPAND:展开节点 "DELETE:删除节点 "MOVE:移动节点 "GET_CHILDREN:获取所有子节点 "GET_FIRST_CHILD:获取第一个子节点 "GET_LAST_CHILD:获取最后子节点 "GET_NEXT_SIBLING:获取下个兄弟节点 "GET_PARENT:获取parent节点 "GET_PREVIOUS_SIBLING:获取上个兄弟节点 lo_node = lo_nodes->add_node( related_node = '' relationship = cl_gui_column_tree=>RELAT_LAST_CHILD data_row = lw_spfli text = 'root node' ). "设置folder lo_node->set_folder( abap_true ). "设置icon lo_node->set_expanded_icon( '@3T@' ). lo_node->set_collapsed_icon( '@3S@' ). "获取key lv_key = lo_node->get_key( ). "获取hierarchy item,设置超链接, lo_item = lo_node->get_hierarchy_item( ). lo_item->set_type( if_salv_c_item_type=>link ). "获取特定列,设置button类型 lo_item = lo_node->get_item( 'CARRID' ). lo_item->set_type( if_salv_c_item_type=>button ). "添加子节点 lo_node = lo_nodes->add_node( related_node = lv_key relationship = cl_gui_column_tree=>RELAT_LAST_CHILD data_row = lw_spfli ). lo_item = lo_node->get_hierarchy_item( ). "设置为checkbox lo_item->set_type( if_salv_c_item_type=>checkbox ). lo_item->set_editable( abap_true ). lo_node = lo_nodes->add_node( related_node = lv_key relationship = cl_gui_column_tree=>RELAT_LAST_CHILD data_row = lw_spfli ). CATCH CX_SALV_MSG. ENDTRY. ENDFORM.
5.8设置selection
代码实例:
"设置selection FORM set_selection. DATA:lo_selection TYPE REF TO cl_salv_selections_tree. DATA:lt_nodes TYPE salv_t_nodes. DATA:ls_nodes TYPE salv_s_nodes. DATA:lt_columns TYPE salv_t_column. DATA:lv_column TYPE LVC_FNAME. DATA:lo_item TYPE REF TO cl_salv_item. "获取selection类对象 lo_selection = lo_tree->get_selections( ). "获取选择节点 lt_nodes = lo_selection->get_selected_nodes( ). "设置选择节点 lo_selection->set_selected_nodes( lt_nodes ). "设置选择列 lt_columns = lo_selection->get_selected_columns( ). "获取选择列 lo_selection->set_selected_columns( lt_columns ). "获取选择item lo_item = lo_selection->get_selected_item( ). "设置选择item lo_selection->set_selected_item( lo_item ). ENDFORM.
5.9设置显示为popup
直接调用cl_salv_tree类对象lo_tree方法set_screen_popup设置为窗口显示,只能是fullscreen方式才能设置为popup显示。
代码实例:
"设置窗口显示,只能是fullscreen方式显示才能设置 FORM set_popup. lo_tree->set_screen_popup( start_column = 10 end_column = 400 start_line = 10 end_line = 500 ). ENDFORM.
5.10关闭窗口
代码实例:
"关闭窗口 FORM set_close_window. lo_tree->close_screen( ). ENDFORM.
5.11设置layout
代码实例:
"设置layout FORM set_layout. DATA:lo_layout TYPE REF TO cl_salv_layout. DATA:lv_key TYPE salv_s_layout_key. "设置key lv_key-report = sy-repid. "获取当前layout lo_layout = lo_tree->get_layout( ). lo_layout->set_key( lv_key ). "获取default lo_layout->get_default_layout( ). "获取init layout lo_layout->get_initial_layout( ). "获取当前layout lo_layout->get_current_layout( ). "设置default lo_layout->set_default( abap_true ). "设置init layout lo_layout->set_initial_layout( 'test' ). "设置save restriction "if_salv_c_layout=>restrict_user_dependant "if_salv_c_layout=>restrict_user_independant. lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ). ENDFORM.
5.12设置汇总
通过cl_salv_tree类对象lo_tree调用方法get_aggregations获取cl_salv_aggregations类对象。
代码实例:
"设置汇总 FORM set_sort_agg. DATA:lo_aggregations TYPE REF TO cl_salv_aggregations. "获取lo_aggregation类对象 lo_aggregations = lo_tree->get_aggregations( ). "清空汇总 lo_aggregations->clear( ). "添加汇总 "汇总规则AGGREGATION "IF_SALV_C_AGGREGATION~TOTAL "IF_SALV_C_AGGREGATION~MINIMUM "IF_SALV_C_AGGREGATION~MAXIMUM "IF_SALV_C_AGGREGATION~AVERAGE "IF_SALV_C_AGGREGATION~NONE TRY . lo_aggregations->add_aggregation( columnname = 'DISTANCE' ). CATCH cx_salv_not_found cx_salv_data_error cx_salv_existing. ENDTRY. ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/13055057.html