系统自带的选择都是单选的,但是需求不一定是单选的,那么需要和选择屏幕一样的范围选择要怎么做呢,以下是一个样例,通过查询物料号来展现物料表的数据。
9000屏幕
创建屏幕
设置屏幕类型
布局编辑
构建一个子屏幕subscreen用于防止选择屏幕,构建一个客制化容器contain用于存放查询结果展现
添加ok_code
9001子屏幕
SELECTION-SCREEN BEGIN OF SCREEN 9001 AS SUBSCREEN.
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME.
SELECT-OPTIONS:
s_matnr FOR mara-matnr.
SELECTION-SCREEN: END OF BLOCK blk1.
SELECTION-SCREEN END OF SCREEN 9001.
9000 PBO&PAI
PROCESS BEFORE OUTPUT.
MODULE STATUS_9000.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_9000.
MODULE user_exit_9000 AT EXIT-COMMAND.
PBO
一般来说PBO 和 PAI 我会分成两个包含文件,也是后期维护的时候便于查找
MODULE status_9000 OUTPUT.
SET PF-STATUS 'STANDARD_9000'.
SET TITLEBAR 'TITLE_9000'.
ENDMODULE.
创建状态栏
其中注意以下 $exit 需要改成退出命令
创建标题
PAI
MODULE user_command_9000 INPUT.
save_ok = ok_code.
CLEAR:ok_code.
CASE save_ok.
WHEN '$BACK'.
LEAVE TO SCREEN 0.
WHEN '$ROLL'.
LEAVE TO SCREEN 0.
WHEN '$SEARCH'.
PERFORM frm_get_data.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
MODULE user_exit_9000 INPUT.
LEAVE PROGRAM.
ENDMODULE.
将子屏幕放入9000屏幕中
代码变更
PROCESS BEFORE OUTPUT.
CALL SUBSCREEN: subscreen INCLUDING sy-repid dynn_9001.
MODULE status_9000.
*
PROCESS AFTER INPUT.
CALL SUBSCREEN: subscreen.
MODULE user_command_9000.
MODULE user_exit_9000 AT EXIT-COMMAND.
选择屏幕展示
到了这里已经完成了一半的工作量了,使用dialog已经可以通过范围选择来进行数据查询了
获取数据
这里就简单的获取数据,展示两个字段即可
FORM frm_get_data .
SELECT
from mara
FIELDS
mara~matnr,
mara~mtart
where mara~matnr in @s_matnr
INTO CORRESPONDING FIELDS OF TABLE @gt_mara.
ENDFORM.
定义并实例化内表
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
END OF ty_mara.
DATA: gt_mara TYPE STANDARD TABLE OF ty_mara.
构建展示容器
PBO代码更新
MODULE status_9000 OUTPUT.
SET PF-STATUS 'STANDARD_9000'.
SET TITLEBAR 'TITLE_9000'.
PERFORM frm_screen.
ENDMODULE.
变量
DATA : wa_container TYPE scrfname VALUE 'CONTAIN',
alv_grid TYPE REF TO cl_gui_alv_grid,
wa_custom_container TYPE REF TO cl_gui_custom_container.
DATA: gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo.
例程frm_screen
PERFORM frm_set_layout. "设置ALV输出格式
PERFORM frm_set_fieldcat. "设置ALV输出字段
PERFORM frm_set_toolbar. "设置alv状态栏
IF wa_custom_container IS INITIAL .
CREATE OBJECT wa_custom_container
EXPORTING
container_name = wa_container.
CREATE OBJECT alv_grid
EXPORTING
i_parent = wa_custom_container.
"实例化方法
* CREATE OBJECT event_receiverundo.
"新增按钮
* SET HANDLER event_receiverundo->handle_toolbar_prod FOR alv_grid_prod.
* SET HANDLER event_receiverundo->handle_ucomm FOR alv_grid_prod.
ENDIF.
CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'GS_PROD'
is_layout = gs_layout
it_toolbar_excluding = gt_ui_func
CHANGING
it_outtab = gt_mara
it_fieldcatalog = gt_fieldcat.
PERFORM frm_refresh_alv USING alv_grid.
例程frm_set_layout
FORM frm_set_layout .
gs_layout = VALUE #( zebra = abap_on
cwidth_opt = abap_on ).
ENDFORM.
例程frm_set_fieldcat
FORM frm_set_fieldcat .
DEFINE _set_fieldcat.
APPEND VALUE #(
fieldname = &1
ref_table = &2
ref_field = &3
coltext = &4
scrtext_l = &4
scrtext_m = &4
scrtext_s = &4
) TO gt_fieldcat.
END-OF-DEFINITION.
_set_fieldcat 'MATNR' 'MARA' 'MATNR' '物料号'. "
_set_fieldcat 'MTART' 'MARA' 'MTART' '物料类型'. "
ENDFORM.
例程frm_set_toolbar
控制容器中alv的按钮
FORM frm_set_toolbar .
DATA: ls_ui_func TYPE ui_func.
REFRESH: gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_copy .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_copy_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_cut .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_refresh .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_move_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_paste .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_paste_new_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_undo .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_pc_file .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_delete_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_insert_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND ls_ui_func TO gt_ui_func.
ENDFORM.
例程frm_refresh_alv
刷新容器中的数据
FORM frm_refresh_alv USING pv_grid TYPE REF TO cl_gui_alv_grid.
CALL METHOD pv_grid->check_changed_data. "检查ALV更改数据
pv_grid->get_frontend_layout( IMPORTING es_layout = DATA(ls_layout) ).
ls_layout-cwidth_opt = abap_on.
pv_grid->set_frontend_layout( is_layout = ls_layout ).
CALL METHOD cl_gui_control=>set_focus
EXPORTING
control = pv_grid.
"刷新变量
DATA: ls_stable TYPE lvc_s_stbl.
"刷新显示
CLEAR ls_stable.
ls_stable-row = 'X'."基于行刷新
ls_stable-col = 'X'."基于列刷新
CALL METHOD pv_grid->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 0.
ENDFORM.
这样就完成了,下过如上面最初展示的那样
以下是源码提供
我这边分了几个包含文件和主文件
主文件代码
*&---------------------------------------------------------------------*
*& Report Y_PNJ_DEMO15
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT y_pnj_demo15.
INCLUDE y_pnj_demo015_top.
INCLUDE y_pnj_demo015_pbo.
INCLUDE y_pnj_demo015_pai.
INCLUDE y_pnj_demo015_per.
TABLES: mara.
"定义 9002的选择屏幕
SELECTION-SCREEN BEGIN OF SCREEN 9001 AS SUBSCREEN.
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME.
SELECT-OPTIONS:
s_matnr FOR mara-matnr.
SELECTION-SCREEN: END OF BLOCK blk1.
SELECTION-SCREEN END OF SCREEN 9001.
*&---------------------------------------------------------------------*
*&INITIALIZATION/选择屏幕前初始化(选择屏幕展示之前执行)
*&---------------------------------------------------------------------*
INITIALIZATION .
*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN OUTPUT/选择屏幕控制(第二屏输出)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT .
*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN/选择屏幕执行(参数输入检查)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN .
*&---------------------------------------------------------------------*
*& START-OF-SELECTION/开始选择屏幕(点击执行按钮之后执行的语块)
*&---------------------------------------------------------------------*
START-OF-SELECTION .
CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*& END-OF-SELECTION/结束选择屏幕(程序结束处理,输出等)
*&---------------------------------------------------------------------*
END-OF-SELECTION .
*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_data .
SELECT
from mara
FIELDS
mara~matnr,
mara~mtart
where mara~matnr in @s_matnr
INTO CORRESPONDING FIELDS OF TABLE @gt_mara.
ENDFORM.
Y_PNJ_DEMO015_TOP
变量文件
*&---------------------------------------------------------------------*
*& 包含 Y_PNJ_DEMO015_TOP
*&---------------------------------------------------------------------*
*&---Variable
DATA: ok_code TYPE sy-ucomm, " 用于接收屏幕操作
save_ok TYPE sy-ucomm. " 接入屏幕操作 并清空ok_code
DATA: dynn_9001(4) VALUE '9001'.
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
END OF ty_mara.
DATA: gt_mara TYPE STANDARD TABLE OF ty_mara,
gs_mara TYPE ty_mara.
"屏幕的容器
DATA : wa_container TYPE scrfname VALUE 'CONTAIN',
alv_grid TYPE REF TO cl_gui_alv_grid,
wa_custom_container TYPE REF TO cl_gui_custom_container.
DATA: gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo.
DATA: gt_ui_func TYPE ui_functions. "隐藏alv菜单控件
Y_PNJ_DEMO015_PBO
PBO
*&---------------------------------------------------------------------*
*& 包含 Y_PNJ_DEMO015_PBO
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'STANDARD_9000'.
SET TITLEBAR 'TITLE_9000'.
PERFORM frm_screen.
ENDMODULE.
Y_PNJ_DEMO015_PAI
PAI
*&---------------------------------------------------------------------*
*& 包含 Y_PNJ_DEMO015_PAI
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
save_ok = ok_code.
CLEAR:ok_code.
CASE save_ok.
WHEN '$BACK'.
LEAVE TO SCREEN 0.
WHEN '$ROLL'.
LEAVE TO SCREEN 0.
WHEN '$SEARCH'.
PERFORM frm_get_data.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_EXIT_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_exit_9000 INPUT.
LEAVE PROGRAM.
ENDMODULE.
Y_PNJ_DEMO015_PER
例程
*&---------------------------------------------------------------------*
*& 包含 Y_PNJ_DEMO015_PER
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form frm_screen
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_screen .
PERFORM frm_set_layout. "设置ALV输出格式
PERFORM frm_set_fieldcat. "设置ALV输出字段
PERFORM frm_set_toolbar. "设置alv状态栏
IF wa_custom_container IS INITIAL .
CREATE OBJECT wa_custom_container
EXPORTING
container_name = wa_container.
CREATE OBJECT alv_grid
EXPORTING
i_parent = wa_custom_container.
"实例化方法
* CREATE OBJECT event_receiverundo.
"新增按钮
* SET HANDLER event_receiverundo->handle_toolbar_prod FOR alv_grid_prod.
* SET HANDLER event_receiverundo->handle_ucomm FOR alv_grid_prod.
CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'GS_MARA'
is_layout = gs_layout
it_toolbar_excluding = gt_ui_func
CHANGING
it_outtab = gt_mara
it_fieldcatalog = gt_fieldcat.
ENDIF.
PERFORM frm_refresh_alv USING alv_grid.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_layout .
gs_layout = VALUE #( zebra = abap_on
cwidth_opt = abap_on ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_fieldcat .
DEFINE _set_fieldcat.
APPEND VALUE #(
fieldname = &1
ref_table = &2
ref_field = &3
coltext = &4
scrtext_l = &4
scrtext_m = &4
scrtext_s = &4
) TO gt_fieldcat.
END-OF-DEFINITION.
_set_fieldcat 'MATNR' 'MARA' 'MATNR' '物料号'. "
_set_fieldcat 'MTART' 'MARA' 'MTART' '物料类型'. "
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_toolbar
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_toolbar .
DATA: ls_ui_func TYPE ui_func.
REFRESH: gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_copy .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_copy_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_cut .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_refresh .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_move_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_paste .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_paste_new_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_undo .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_pc_file .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_delete_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_insert_row .
APPEND ls_ui_func TO gt_ui_func.
ls_ui_func = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND ls_ui_func TO gt_ui_func.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_refresh_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> ALV_GRID
*&---------------------------------------------------------------------*
FORM frm_refresh_alv USING pv_grid TYPE REF TO cl_gui_alv_grid.
CALL METHOD pv_grid->check_changed_data. "检查ALV更改数据
pv_grid->get_frontend_layout( IMPORTING es_layout = DATA(ls_layout) ).
ls_layout-cwidth_opt = abap_on.
pv_grid->set_frontend_layout( is_layout = ls_layout ).
CALL METHOD cl_gui_control=>set_focus
EXPORTING
control = pv_grid.
"刷新变量
DATA: ls_stable TYPE lvc_s_stbl.
"刷新显示
CLEAR ls_stable.
ls_stable-row = 'X'."基于行刷新
ls_stable-col = 'X'."基于列刷新
CALL METHOD pv_grid->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 0.
ENDFORM.
标签:dialog,源码,ui,ls,func,alv,grid,SAP,&-----------------------------------------
From: https://www.cnblogs.com/pnj-owowa/p/17984569