个人TREE ALV 模版-加快你的开发
上次发了OO ALV模板(个人OO ALV 模版-加快你的ALV开发)后,小伙伴们用的可舒服的,这次整理下tree alv的模板
1. 我们随便来点数据
代码语言:javascript复制SELECT * FROM sflight INTO TABLE @gt_sflight UP TO 50 ROWS.
2. 创建屏幕
这里我们行列给到最大值200255
然后在绘制界面中给一个容器CONTAINER1(长高也是255200,同时给到
)这样容器就会根据你电脑分表率大小自动调节了 对应创建OK_CODE
3. 添加逻辑流
代码语言:javascript复制MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
IF g_alv_tree IS INITIAL.
PERFORM init_tree.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2.
ASSERT sy-subrc = 0.
ENDIF.
ENDMODULE.
MODULE pai INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
CALL METHOD g_alv_tree->r_container->free.
LEAVE PROGRAM.
WHEN OTHERS.
CALL METHOD cl_gui_cfw=>dispatch.
ENDCASE.
CALL METHOD cl_gui_cfw=>flush.
ENDMODULE.
顺便加上Status:
4. Tree ALV 调用示例
输入以下代码,然后直接点击执行程序,我们可以看到一个ALV tree
代码语言:javascript复制*&=====================================================================*
*& Created date: 2022.05.11
*& Created by: Tab-Zhu (微信公众号:Tab_JXZHU)
*&---------------------------------------------------------------------*
*& Changed date:
*& Changed by:
*&---------------------------------------------------------------------*
REPORT ztab_test_02.
TYPES: BEGIN OF ty_tree_node,
id TYPE char18,
text TYPE char40,
END OF ty_tree_node.
DATA: g_alv_tree TYPE REF TO zcl_tab_tree_alv.
DATA: gt_sflight TYPE sflight OCCURS 0, "Output-Table
gt_scala TYPE STANDARD TABLE OF ty_tree_node,
ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm, "OK-Code
g_max TYPE i VALUE 255.
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE @gt_sflight UP TO 50 ROWS.
CHECK sy-subrc EQ 0.
END-OF-SELECTION.
CALL SCREEN 9000.
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
IF g_alv_tree IS INITIAL.
PERFORM init_tree.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2.
ASSERT sy-subrc = 0.
ENDIF.
ENDMODULE. " PBO OUTPUT
MODULE pai INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
CALL METHOD g_alv_tree->r_container->free.
LEAVE PROGRAM.
WHEN OTHERS.
CALL METHOD cl_gui_cfw=>dispatch.
ENDCASE.
CALL METHOD cl_gui_cfw=>flush.
ENDMODULE. " PAI INPUT
FORM init_tree.
CREATE OBJECT g_alv_tree.
DATA(ls_alv_tree_head) = VALUE treev_hhdr( heading = '航班' tooltip = '航班'
width = 35 width_pix = space ).
CALL METHOD zcl_tab_tree_alv=>set_default_fieldcat
CHANGING
it_data = gt_sflight
RECEIVING
rt_fieldcat = DATA(lt_fieldcat)
EXCEPTIONS
error_in_get_fieldcat = 1
OTHERS = 2.
IF sy-subrc <> 0.
CLEAR lt_fieldcat[].
ENDIF.
LOOP AT lt_fieldcat ASSIGNING FIELD-SYMBOL(<fs_fcat>).
DATA(lv_tabix) = sy-tabix.
CASE <fs_fcat>-fieldname.
WHEN 'MANDT'.
DELETE lt_fieldcat INDEX lv_tabix.
WHEN 'CARRID'.
<fs_fcat>-outputlen = 10.
WHEN 'CONNID'.
<fs_fcat>-outputlen = 10.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
CALL METHOD g_alv_tree->create_tree_alv
EXPORTING
* iv_repid = SY-CPROG
* iv_screen = '9000'
* iv_item_selection = 'X'
* iv_no_html_header = 'X'
iv_hierarchy_header = ls_alv_tree_head
iv_container_name = 'CONTAINER1'
* iv_variant_handle = '1'
it_fieldcat = lt_fieldcat
i_set_tree_node_form = 'FRM_SET_TREE_NODE_FORM'
* i_set_toolbar_form =
* i_function_selected_form =
* i_dropdown_clicked_form =
* i_checkbox_change_form =
* i_link_click_form =
CHANGING
ct_data = gt_sflight
EXCEPTIONS
create_tree_alv_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE x208(00) WITH 'Create tree alv failed'.
ENDIF.
ENDFORM.
FORM frm_set_tree_node_form
CHANGING cr_alv_tree TYPE REF TO cl_gui_alv_tree
ct_data TYPE STANDARD TABLE.
DATA:lt_data TYPE TABLE OF sflight."inner table structure ex:sflight 以航班表为例子
DATA:ls_output_line TYPE sflight.
DATA:ls_layout_node TYPE lvc_s_layn.
DATA: ls_layout_item TYPE lvc_s_layi,
lt_layout_item TYPE lvc_t_layi.
DATA:lv_top_key TYPE lvc_nkey.
DATA:lv_next_key TYPE lvc_nkey.
DATA:lv_node_text TYPE lvc_value.
lt_data = ct_data.
CHECK lt_data IS NOT INITIAL.
LOOP AT lt_data INTO DATA(ls_data) GROUP BY (
carrid = ls_data-carrid )
ASSIGNING FIELD-SYMBOL(<fs_group>).
"HEADER
"层级名称
lv_node_text = 'LEVEL 1'.
* "选择框
* CLEAR ls_layout_item.
* ls_layout_item-fieldname = cr_alv_tree->c_hierarchy_column_name."层级名称
* ls_layout_item-class = cl_gui_column_tree=>item_class_checkbox.
* ls_layout_item-editable = 'X'.
* APPEND ls_layout_item TO lt_layout_item.
* "设置图标
* CLEAR ls_layout_item.
* ls_layout_item-fieldname = 'STATUS'."对于某个字段设置为图标
* ls_layout_item-t_image = '@09@'."yellow
* APPEND ls_layout_item TO lt_layout_item.
CLEAR ls_output_line.
MOVE-CORRESPONDING <fs_group> TO ls_output_line.
CALL METHOD cr_alv_tree->add_node
EXPORTING
i_relat_node_key = ''
i_relationship = cl_gui_column_tree=>relat_last_child "how to insert the node
i_node_text = lv_node_text "hierarchy node text
* is_node_layout = ls_layout_node "node layout
is_outtab_line = ls_output_line "attributes of the inserted node
* it_item_layout = lt_layout_item "item layout
IMPORTING
e_new_node_key = lv_top_key.
LOOP AT GROUP <fs_group> ASSIGNING FIELD-SYMBOL(<fs_line>).
"item
CLEAR ls_output_line.
MOVE-CORRESPONDING <fs_line> TO ls_output_line.
lv_node_text = 'LEVEL 2'.
CALL METHOD cr_alv_tree->add_node
EXPORTING
i_relat_node_key = lv_top_key "Top key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = lv_node_text
is_outtab_line = ls_output_line
* is_node_layout = ls_layout_node
* it_item_layout = lt_layout_item
IMPORTING
e_new_node_key = lv_next_key.
ENDLOOP.
ENDLOOP.
ENDFORM.
5. 模板代码
下面介绍ZCL_TAB_TREE_ALV代码- 复制可用 小伙伴们可以直接SE24,创建一个类,然后点击基于源代码创建,然后COPY代码即可
代码语言:javascript复制*&=====================================================================*
*& Created date: 2022.05.11
*& Created by: Tab-Zhu (微信公众号:Tab_JXZHU)
*&---------------------------------------------------------------------*
*& Changed date:
*& Changed by:
*&---------------------------------------------------------------------*CLASS zcl_tab_tree_alv DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
DATA r_container TYPE REF TO cl_gui_custom_container .
DATA r_alv_tree TYPE REF TO cl_gui_alv_tree .
DATA r_toolbar TYPE REF TO cl_gui_toolbar.
DATA r_line_behaviour TYPE REF TO cl_dragdrop .
DATA r_fav_behaviour TYPE REF TO cl_dragdrop .
DATA m_repid TYPE progname .
DATA:
m_set_toolbar_form TYPE string,
m_set_tree_node_form TYPE string.
DATA:
m_function_selected_form TYPE string,
m_dropdown_clicked_form TYPE string,
m_checkbox_change_form TYPE string,
m_link_click_form TYPE string.
METHODS create_tree_alv
IMPORTING
VALUE(iv_repid) TYPE progname DEFAULT sy-cprog
VALUE(iv_screen) TYPE sy-dynnr DEFAULT '9000'
VALUE(iv_item_selection) TYPE flag DEFAULT 'X'"
VALUE(iv_no_html_header) TYPE flag DEFAULT 'X'"
VALUE(iv_hierarchy_header) TYPE treev_hhdr "树控制:层次结构表头的属性
VALUE(iv_container_name) TYPE c DEFAULT 'CONTAINER'
VALUE(iv_variant_handle) TYPE disvariant-handle DEFAULT '1'
VALUE(it_fieldcat) TYPE lvc_t_fcat OPTIONAL
!i_set_tree_node_form TYPE string
!i_set_toolbar_form TYPE string OPTIONAL
!i_function_selected_form TYPE string OPTIONAL
!i_dropdown_clicked_form TYPE string OPTIONAL
!i_checkbox_change_form TYPE string OPTIONAL
!i_link_click_form TYPE string OPTIONAL
CHANGING
!ct_data TYPE STANDARD TABLE
EXCEPTIONS
create_tree_alv_failed .
CLASS-METHODS set_default_fieldcat
CHANGING
!it_data TYPE STANDARD TABLE
RETURNING
VALUE(rt_fieldcat) TYPE lvc_t_fcat
EXCEPTIONS
error_in_get_fieldcat .
"set toolbar
METHODS set_toolbar
CHANGING
cr_toolbar TYPE REF TO cl_gui_toolbar.
"set tree node
METHODS set_tree_node
CHANGING
!ct_data TYPE STANDARD TABLE
!cr_alv_tree TYPE REF TO cl_gui_alv_tree.
"function_selected 按钮功能
METHODS handle_function_selected FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode.
"dropdown_clicked 功能组
METHODS handle_dropdown_clicked FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode posx posy.
"checkbox_change 选择框
METHODS handle_checkbox_change FOR EVENT checkbox_change OF cl_gui_alv_tree
IMPORTING checked fieldname node_key.
"link_click 点击链接
METHODS handle_link_click FOR EVENT link_click OF cl_gui_alv_tree
IMPORTING node_key fieldname.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS ZCL_TAB_TREE_ALV IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->CREATE_TREE_ALV
* -------------------------------------------------------------------------------------------------
* | [--->] IV_REPID TYPE PROGNAME (default =SY-CPROG)
* | [--->] IV_SCREEN TYPE SY-DYNNR (default ='9000')
* | [--->] IV_ITEM_SELECTION TYPE FLAG (default ='X')
* | [--->] IV_NO_HTML_HEADER TYPE FLAG (default ='X')
* | [--->] IV_HIERARCHY_HEADER TYPE TREEV_HHDR
* | [--->] IV_CONTAINER_NAME TYPE C (default ='CONTAINER')
* | [--->] IV_VARIANT_HANDLE TYPE DISVARIANT-HANDLE (default ='1')
* | [--->] IT_FIELDCAT TYPE LVC_T_FCAT(optional)
* | [--->] I_SET_TREE_NODE_FORM TYPE STRING
* | [--->] I_SET_TOOLBAR_FORM TYPE STRING(optional)
* | [--->] I_FUNCTION_SELECTED_FORM TYPE STRING(optional)
* | [--->] I_DROPDOWN_CLICKED_FORM TYPE STRING(optional)
* | [--->] I_CHECKBOX_CHANGE_FORM TYPE STRING(optional)
* | [--->] I_LINK_CLICK_FORM TYPE STRING(optional)
* | [<-->] CT_DATA TYPE STANDARD TABLE
* | [EXC!] CREATE_TREE_ALV_FAILED
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD create_tree_alv.
DATA:ls_variant TYPE disvariant.
DATA lt_filedcat TYPE lvc_t_fcat.
m_repid = iv_repid.
CREATE OBJECT r_container
EXPORTING
container_name = iv_container_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
IF sy-subrc <> 0.
RAISE create_tree_alv_failed.
ENDIF.
CREATE OBJECT r_alv_tree
EXPORTING
parent = r_container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = iv_item_selection
no_html_header = iv_no_html_header
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.
RAISE create_tree_alv_failed.
ENDIF.
"字段目录
IF it_fieldcat[] IS NOT INITIAL.
lt_filedcat = it_fieldcat.
ELSE.
CALL METHOD me->set_default_fieldcat
CHANGING
it_data = ct_data
RECEIVING
rt_fieldcat = lt_filedcat
EXCEPTIONS
error_in_get_fieldcat = 1
OTHERS = 2.
IF sy-subrc NE 0.
MESSAGE e368(00) WITH 'Failed to get fiedlcat' ''.
ENDIF.
ENDIF.
ls_variant-report = iv_repid.
ls_variant-handle = iv_variant_handle.
ls_variant-username = sy-uname.
DATA lr_data TYPE REF TO data.
FIELD-SYMBOLS <fs_table> TYPE STANDARD TABLE.
CREATE DATA lr_data LIKE ct_data.
ASSIGN lr_data->* TO <fs_table>.
CALL METHOD r_alv_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = iv_hierarchy_header
is_variant = ls_variant
i_save = 'A'
CHANGING
it_fieldcatalog = lt_filedcat
it_outtab = <fs_table>. "lt_data 必须为空
"get toolbar class
CALL METHOD r_alv_tree->get_toolbar_object
IMPORTING
er_toolbar = r_toolbar.
"set toolbar 设置工具栏
m_set_toolbar_form = i_set_toolbar_form.
CALL METHOD me->set_toolbar
CHANGING
cr_toolbar = r_toolbar.
"set tree node 设置树形结构
m_set_tree_node_form = i_set_tree_node_form.
CALL METHOD me->set_tree_node
CHANGING
cr_alv_tree = r_alv_tree
ct_data = ct_data.
"register events 注册事件
DATA:
lt_events TYPE cntl_simple_events,
ls_events TYPE cntl_simple_event.
CALL METHOD r_alv_tree->get_registered_events
IMPORTING
events = lt_events.
ls_events-eventid = cl_gui_column_tree=>eventid_link_click.
ls_events-appl_event = 'X'.
APPEND ls_events TO lt_events.
ls_events-eventid = cl_gui_column_tree=>eventid_checkbox_change.
ls_events-appl_event = 'X'.
APPEND ls_events TO lt_events.
CALL METHOD r_alv_tree->set_registered_events
EXPORTING
events = lt_events
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.
IF sy-subrc <> 0.
RAISE create_tree_alv_failed.
ENDIF.
*SET EVENT
DEFINE d_set_handler.
IF &1 IS SUPPLIED.
&2 = &1.
SET HANDLER me->&3 FOR &4.
ENDIF.
END-OF-DEFINITION.
d_set_handler:
i_function_selected_form m_function_selected_form handle_function_selected r_toolbar,"按钮功能
i_dropdown_clicked_form m_dropdown_clicked_form handle_dropdown_clicked r_toolbar,"工具栏组合
i_checkbox_change_form m_checkbox_change_form handle_checkbox_change r_alv_tree,"选择框
i_link_click_form m_link_click_form handle_link_click r_alv_tree."点击链接
* SET HANDLER me->click_button_action FOR r_toolbar."按钮功能
* SET HANDLER me->on_dropdown_clicked FOR r_toolbar."工具栏组合
* SET HANDLER me->handle_checkbox_change FOR r_alv_tree."选择框
* SET HANDLER me->on_link_click FOR r_alv_tree."点击链接
CALL METHOD r_alv_tree->update_calculations.
CALL METHOD r_alv_tree->frontend_update.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2.
IF sy-subrc NE 0.
RAISE create_tree_alv_failed.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->HANDLE_CHECKBOX_CHANGE
* -------------------------------------------------------------------------------------------------
* | [--->] CHECKED LIKE
* | [--->] FIELDNAME LIKE
* | [--->] NODE_KEY LIKE
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD handle_checkbox_change.
PERFORM (m_checkbox_change_form) IN PROGRAM (m_repid) IF FOUND
USING checked fieldname node_key.
*FORM 示例
*FORM m_checkbox_change USING checked
* fieldname
* node_key.
* DATA:
* lv_parent_key TYPE lvc_nkey,
* ls_laci TYPE lvc_s_laci,
* lt_laci TYPE lvc_t_laci,
* lt_children_keys TYPE lvc_t_nkey,
* ls_layout_item TYPE lvc_s_layi,
* lt_layout_item TYPE lvc_t_layi.
* "得到该节点的父节点
* CALL METHOD r_alv_tree->get_parent
* EXPORTING
* i_node_key = node_key
* IMPORTING
* e_parent_node_key = lv_parent_key.
* IF lv_parent_key EQ '&VIRTUALROOT'."根目录
* "该节点的子节点
* CALL METHOD r_alv_tree->get_children
* EXPORTING
* i_node_key = node_key
* IMPORTING
* et_children = lt_children_keys.
* "将该节点下方的
* LOOP AT lt_children_keys INTO DATA(ls_children_key).
* ls_laci-fieldname = cl_alv_tree_base=>c_hierarchy_column_name.
* ls_laci-chosen = checked.
* ls_laci-u_chosen = gc_x.
* CALL METHOD r_alv_tree->change_item
* EXPORTING
* i_node_key = ls_children_key
* i_fieldname = cl_alv_tree_base=>c_hierarchy_column_name
* i_data = ''
* i_u_data = ''
* is_item_layout = ls_laci.
* ENDLOOP.
* "当子节点置空时, 需要将父节点置空
* IF lv_parent_key NE '&VIRTUALROOT' AND checked IS INITIAL.
* ls_laci-fieldname = cl_alv_tree_base=>c_hierarchy_column_name.
* ls_laci-chosen = checked.
* ls_laci-u_chosen = gc_x.
* CALL METHOD r_alv_tree->change_item
* EXPORTING
* i_node_key = lv_parent_key
* i_fieldname = cl_alv_tree_base=>c_hierarchy_column_name
* i_data = ''
* i_u_data = ''
* is_item_layout = ls_laci.
* ENDIF.
* ENDIF.
* CALL METHOD r_alv_tree->frontend_update.
*ENDFORM.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->HANDLE_DROPDOWN_CLICKED
* -------------------------------------------------------------------------------------------------
* | [--->] FCODE LIKE
* | [--->] POSX LIKE
* | [--->] POSY LIKE
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD handle_dropdown_clicked.
PERFORM (m_dropdown_clicked_form) IN PROGRAM (m_repid) IF FOUND
USING fcode posx posy.
*FORM 示例
*FORM m_dropdown_clicked USING fcode TYPE UI_FUNC
* posx TYPE I
* posy TYPE I.
*-- Fires when a dropdown menu is clicked. After it has been
*-- clicked a context menu is shown beside the button.
* Save x and y position of button for use with context menu
* Create context menu for menu button
* DATA: lo_submenu TYPE REF TO cl_ctmenu.
* DATA: lo_menu TYPE REF TO cl_ctmenu.
* IF lo_menu IS INITIAL.
* CREATE OBJECT lo_menu.
* CASE uv_fcode.
* WHEN 'MENU1'."这个按钮是通过method中 set_toolbar :cr_tree_alv->add_function
* "下拉按钮1
* CALL METHOD lo_menu->add_function
* EXPORTING
* fcode = 'DOWNHU'
* icon = icon_export
* text = 'DOWNLOAD'(088).
* "下拉按钮2
* CALL METHOD lo_menu->add_function
* EXPORTING
* fcode = 'UPHU'
* icon = icon_import
* text = 'UPLOAD'(089).
* WHEN OTHERS.
* ENDCASE.
******如果仍有需要
****** CALL METHOD gr_context_menu->add_separator.
****** create sub menu for the context menu
****** CREATE OBJECT lo_submenu.
****** CALL METHOD lo_submenu->add_function
****** EXPORTING
****** fcode = 'UPMAT'
****** icon = icon_import
****** butn_type = lv_btype
****** text = TEXT-091
****** quickinfo = TEXT-091.
****** CALL METHOD lo_submenu->add_function
****** EXPORTING
****** fcode = 'SUB2'
****** text = 'Do something good'.
*******-- ADD sub menu to the context menu
****** CALL METHOD lo_menu->add_submenu
****** EXPORTING
****** menu = lo_submenu
****** text = 'Do something else.....'.
****** ENDIF.
* CALL METHOD r_toolbar->track_context_menu
* EXPORTING
* context_menu = lo_menu
* posx = posx
* posy = posy.
*ENDFORM.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->HANDLE_FUNCTION_SELECTED
* -------------------------------------------------------------------------------------------------
* | [--->] FCODE LIKE
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD handle_function_selected.
PERFORM (m_function_selected_form) IN PROGRAM (m_repid) IF FOUND
USING fcode.
*FORM 示例
*FORM m_function_selected USING FCODE TYPE UI_FUNC.
* CASE fcode.
* WHEN 'SELALL'.
*
* WHEN 'DESEL'.
*
* WHEN 'MENU1'
* WHEN OTHERS.
* ENDCASE.
*ENDFORM.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->HANDLE_LINK_CLICK
* -------------------------------------------------------------------------------------------------
* | [--->] NODE_KEY LIKE
* | [--->] FIELDNAME LIKE
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD handle_link_click.
PERFORM (m_link_click_form) IN PROGRAM (m_repid) IF FOUND USING node_key fieldname.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Static Public Method ZCL_TAB_TREE_ALV=>SET_DEFAULT_FIELDCAT
* -------------------------------------------------------------------------------------------------
* | [<-->] IT_DATA TYPE STANDARD TABLE
* | [<-()] RT_FIELDCAT TYPE LVC_T_FCAT
* | [EXC!] ERROR_IN_GET_FIELDCAT
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD set_default_fieldcat.
DATA: salv_table TYPE REF TO cl_salv_table.
TRY.
cl_salv_table=>factory( IMPORTING
r_salv_table = salv_table
CHANGING
t_table = it_data[] ).
rt_fieldcat = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = salv_table->get_columns( ) " ALV Filter
r_aggregations = salv_table->get_aggregations( ) " ALV Aggregations
).
CATCH cx_root INTO DATA(lx_root).
DATA(lv_text) = lx_root->get_text( ).
RAISE error_in_get_fieldcat.
ENDTRY.
"当你的内表字段不是参考数据库结构/表创建的时候,F4搜索帮助有图标但是无效
"有2种处理方法
"1. F4AVAILABL = abap_false
"2. 加上:REF_TABLE ,REF_FIELD
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->SET_TOOLBAR
* -------------------------------------------------------------------------------------------------
* | [<-->] CR_TOOLBAR TYPE REF TO CL_GUI_TOOLBAR
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD set_toolbar.
PERFORM (m_set_toolbar_form) IN PROGRAM (m_repid) IF FOUND
CHANGING cr_toolbar.
*FORM 示例
*FORM m_set_toolbar_form CHANGING cr_toolbar TYPE REF TO cl_gui_toolbar.
* "删除
* CALL METHOD cr_toolbar->delete_button
* EXPORTING
* fcode = cl_gui_alv_tree=>mc_fc_print_back.
*
* "增加按钮
* CALL METHOD cr_toolbar->add_button
* EXPORTING
* fcode = 'EXP'
* icon = icon_expand
* butn_type = lv_btype "0 按钮(正常) 1 菜单和缺省按钮 2 菜单 3 分隔符 4 单选按钮 5 复选框 6 菜单选项
* quickinfo = 'quickinfo'.
*ENDFORM.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------
* | Instance Public Method ZCL_TAB_TREE_ALV->SET_TREE_NODE
* -------------------------------------------------------------------------------------------------
* | [<-->] CT_DATA TYPE STANDARD TABLE
* | [<-->] CR_ALV_TREE TYPE REF TO CL_GUI_ALV_TREE
* --------------------------------------------------------------------------------------</SIGNATURE>
METHOD set_tree_node.
PERFORM (m_set_tree_node_form) IN PROGRAM (m_repid) IF FOUND
CHANGING cr_alv_tree ct_data.
*FROM 示例
*FROM m_set_tree_node CHANGING cr_alv_tree TYPE REF TO CL_GUI_ALV_TREE
* ct_data type standard table.
* DATA:lt_data TYPE TABLE OF sflight."inner table structure ex:sflight 以航班表为例子
* DATA:ls_output_line TYPE sflight.
* DATA:ls_layout_node TYPE lvc_s_layn.
* DATA: ls_layout_item TYPE lvc_s_layi,
* lt_layout_item TYPE lvc_t_layi.
* DATA:lv_top_key TYPE lvc_nkey.
* DATA:lv_next_key TYPE lvc_nkey.
* DATA:lv_node_text TYPE lvc_value.
*
* lt_data = ct_data.
*
* CHECK lt_data IS NOT INITIAL.
* LOOP AT lt_data INTO DATA(ls_data) GROUP BY (
* carrid = ls_data-carrid
* connid = ls_data-connid
* fldate = ls_data-fldate )
* ASSIGNING FIELD-SYMBOL(<fs_group>).
*
* "HEADER
* "层级名称
* lv_node_text = 'LEVEL 1'.
* "选择框
* CLEAR ls_layout_item.
* ls_layout_item-fieldname = cr_alv_tree->c_hierarchy_column_name."层级名称
* ls_layout_item-class = cl_gui_column_tree=>item_class_checkbox.
* ls_layout_item-editable = 'X'.
* APPEND ls_layout_item TO lt_layout_item.
*
* "设置图标
* CLEAR ls_layout_item.
* ls_layout_item-fieldname = 'STATUS'."对于某个字段设置为图标
* ls_layout_item-t_image = '@09@'."yellow
* APPEND ls_layout_item TO lt_layout_item.
*
* CALL METHOD cr_alv_tree->add_node
* EXPORTING
* i_relat_node_key = ''
* i_relationship = cl_gui_column_tree=>relat_last_child "how to insert the node
* i_node_text = lv_node_text "hierarchy node text
* is_node_layout = ls_layout_node "node layout
* is_outtab_line = ls_output_line "attributes of the inserted node
* it_item_layout = lt_layout_item "item layout
* IMPORTING
* e_new_node_key = lv_top_key.
* "item
* lv_node_text = 'LEVEL 2'.
* CALL METHOD cr_alv_tree->add_node
* EXPORTING
* i_relat_node_key = lv_top_key "Top key
* i_relationship = cl_gui_column_tree=>relat_last_child
* i_node_text = lv_node_text
* is_outtab_line = ls_output_line
* is_node_layout = ls_layout_node
* it_item_layout = lt_layout_item
* IMPORTING
* e_new_node_key = lv_next_key.
* ENDLOOP.
*ENDFORM.
ENDMETHOD.
ENDCLASS.
希望对大家的开发有帮助哈
更多其他的文章请点击历史记录
ABAP随笔-入门简单ALV-(附单元格颜色控制)
ABAP随笔-获取标准报表数据(MB52)数据进行客制ALV
ABAP随笔-ALV上的下拉框