ABAP FUNCTION ALV中如何在状态栏添加下拉框按钮
时间: 2023-12-24 20:04:27 浏览: 189
在 ABAP FUNCTION ALV 中添加状态栏下拉框按钮,可以使用函数模块 `REUSE_ALV_HIERSEQ_LIST_DISPLAY`。该函数模块可以在状态栏中添加下拉框按钮,用于切换不同的层次结构。
以下是添加下拉框按钮的代码示例:
```
REPORT ztest.
DATA: it_list TYPE STANDARD TABLE OF sflight,
wa_list TYPE sflight.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv.
FIELD-SYMBOLS: <fs_fieldcat> TYPE slis_fieldcat_alv.
* 初始化 ALV 数据
SELECT * FROM sflight INTO TABLE it_list.
* 初始化字段目录
CLEAR: it_fieldcat.
wa_fieldcat-fieldname = 'CARRID'.
wa_fieldcat-ref_table = 'SCARR'.
wa_fieldcat-ref_field = 'CARRID'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'CONNID'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'FLDATE'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'SEATSMAX'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'SEATSOCC'.
APPEND wa_fieldcat TO it_fieldcat.
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'PF_STATUS'
is_layout = VALUE #( ( hierarchy_seq = 'CARRID' ) )
TABLES
t_fieldcat = it_fieldcat
t_outtab = it_list.
* 定义层次结构下拉框的值集合
DATA: it_hier TYPE STANDARD TABLE OF lvc_s_layo WITH HEADER LINE.
it_hier-fieldname = 'HIERARCHY_SEQ'.
it_hier-seltext_l = '层次结构'.
it_hier-selopt = 'O'.
it_hier-no_out = 'X'.
APPEND it_hier.
it_hier-fieldname = 'CARRID'.
it_hier-seltext_l = '按航空公司'.
it_hier-selopt = 'C'.
it_hier-no_out = ' '.
APPEND it_hier.
it_hier-fieldname = 'FLDATE'.
it_hier-seltext_l = '按航班日期'.
it_hier-selopt = 'D'.
it_hier-no_out = ' '.
APPEND it_hier.
it_hier-fieldname = 'SEATSMAX'.
it_hier-seltext_l = '按最大座位数'.
it_hier-selopt = 'S'.
it_hier-no_out = ' '.
APPEND it_hier.
it_hier-fieldname = 'SEATSOCC'.
it_hier-seltext_l = '按已占用座位数'.
it_hier-selopt = 'O'.
it_hier-no_out = ' '.
APPEND it_hier.
* 设置层次结构下拉框
SET PF-STATUS 'PF_STATUS' EXCLUDING it_hier.
* PF-STATUS 事件处理程序
MODULE pf_status OUTPUT.
SET PF-STATUS 'PF_STATUS'.
SET TITLEBAR 'TITLE'.
ENDMODULE.
* AT SELECTION-SCREEN 事件处理程序
AT SELECTION-SCREEN ON HIERARCHY_SEQ.
LOOP AT it_hier ASSIGNING FIELD-SYMBOL(<fs_hier>).
IF <fs_hier>-fieldname = 'HIERARCHY_SEQ'.
CONTINUE.
ENDIF.
IF <fs_hier>-selopt = 'X'.
CLEAR: it_list.
SELECT * FROM sflight INTO TABLE it_list ORDER BY carrid.
ELSE.
CLEAR: it_list.
SELECT * FROM sflight INTO TABLE it_list ORDER BY ( <fs_hier>-fieldname ).
ENDIF.
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'PF_STATUS'
is_layout = VALUE #( ( hierarchy_seq = <fs_hier>-fieldname ) )
TABLES
t_fieldcat = it_fieldcat
t_hierarchy = it_list.
ENDLOOP.
```
在代码中,使用了 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块来显示 ALV 报表,并在 `is_layout` 参数中指定默认的层次结构。在 `pf_status` 事件处理程序中设置状态栏的标题,并在 `AT SELECTION-SCREEN` 事件处理程序中根据用户选择的层次结构重新查询数据并刷新 ALV 报表。
通过在 `it_hier` 中定义层次结构下拉框的值集合,并在 `SET PF-STATUS` 语句中排除该值集合,即可在状态栏中添加下拉框按钮。在 `AT SELECTION-SCREEN` 事件处理程序中,根据用户选择的层次结构重新查询数据,并使用 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块刷新 ALV 报表显示。
阅读全文