SAP实现alv报表通过按钮时间隐藏或显示字段
时间: 2024-05-27 09:14:47 浏览: 205
实现方法:
1. 在ALV报表的布局中,添加一个隐藏/显示字段的按钮。
2. 在程序中定义一个全局变量,用于记录当前字段的显示状态,初始值为真(即显示)。
3. 在按钮的点击事件中,判断当前的显示状态,如果为真,则将需要隐藏的字段的可见性设为假,并将按钮的文字改为“显示”,同时将全局变量的值改为假;如果为假,则将需要显示的字段的可见性设为真,并将按钮的文字改为“隐藏”,同时将全局变量的值改为真。
4. 最后调用REUSE_ALV_GRID_DISPLAY函数刷新ALV报表即可。
示例代码如下:
DATA: gv_hide_field TYPE abap_bool VALUE 'X'.
* 定义按钮事件处理程序
DATA: lcl_event_receiver TYPE REF TO cl_gui_alv_grid.
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS: on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_alv_toolbar.
ENDCLASS.
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD on_toolbar.
DATA: l_button TYPE salv_s_toolbar_button.
* 添加一个隐藏/显示字段的按钮
l_button-function = 'HIDE_SHOW_FIELD'.
l_button-icon = 'ICON_HIDE_SHOW'.
l_button-quickinfo = 'Hide/Show Field'.
l_button-text = 'Hide Field'.
l_button-user_command = 'HIDE_SHOW_FIELD'.
APPEND l_button TO e_alv_toolbar->salv_toolbar.
ENDMETHOD.
ENDCLASS.
* 定义隐藏/显示字段的按钮事件处理程序
DATA: lcl_event_handler TYPE REF TO lcl_event_receiver.
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm e_selfield.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION.
METHOD on_user_command.
DATA: l_layout TYPE lvc_s_layo.
CASE e_ucomm.
WHEN 'HIDE_SHOW_FIELD'.
* 判断当前显示状态
IF gv_hide_field = abap_true.
l_layout-hide = abap_true. " 需要隐藏的字段
e_selfield-layout = l_layout.
e_selfield-refresh = abap_true.
e_selfield-free = abap_true.
SET CURSOR FIELD 'HIDE_SHOW_FIELD'.
e_selfield-text = 'Show Field'.
gv_hide_field = abap_false.
ELSE.
l_layout-hide = abap_false. " 需要显示的字段
e_selfield-layout = l_layout.
e_selfield-refresh = abap_true.
e_selfield-free = abap_true.
SET CURSOR FIELD 'HIDE_SHOW_FIELD'.
e_selfield-text = 'Hide Field'.
gv_hide_field = abap_true.
ENDIF.
ENDCASE.
ENDMETHOD.
ENDCLASS.
* 初始化报表
DATA: gt_outtab TYPE TABLE OF <your_data_type>.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_top_of_page = 'TOP_OF_PAGE'
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'PF_STATUS_SET'
is_layout = gs_layout
TABLES
t_outtab = gt_outtab
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* 添加隐藏/显示字段的按钮
CREATE OBJECT lcl_event_receiver.
* 添加按钮事件处理程序
CREATE OBJECT lcl_event_handler.
SET HANDLER lcl_event_handler->on_user_command FOR lcl_event_receiver.
阅读全文